一篇Objective-C(最佳)实践的文章 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
fly2never
V2EX    iDev

一篇Objective-C(最佳)实践的文章

  •  
  •   fly2never 2012 年 4 月 3 日 7284 次点击
    这是一个创建于 5136 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://ironwolf.dangerousgames.com/blog/archives/913
    仔细读了一遍,很是赞同,特别是对团队开发,内存使用容易错误的地方规范了一下,减少内存问题出错的可能.
    11 条回复    1970-01-01 08:00:00 +08:00
    MartianZ
        1
    MartianZ  
       2012 年 4 月 3 日
    真不错,受益匪浅

    感谢楼主分享
    ultragtx
        2
    ultragtx  
       2012 年 4 月 11 日
    这两天按着这个改了改习惯,发现@implementation里面如果照他说的
    “ALWAYS create a @property for every data member and use “self.name” to access it throughout your class implementation. NEVER access your own data members directly.”
    全部用self.myobj访问来获得成员的值,单步调试Step Into的时候简直就是个悲剧 每次都要跳到@synthesize一次

    赋值的时候是应该用self调用setter,但是获取变量的值的话感觉完全没有必要用self,而且一般情况下很少手写setter和getter,个别几个特殊的注意一下就行了,追求一致性也不至于到这个份上,还是直接用 _myobj 或者 myobj_ 这种方式访问比较靠谱
    xesique
        3
    xesique  
       2012 年 4 月 11 日
    楼上的@ 含义变了,有没有转义的@ 符号啊?
    试试
    \@implementation
    fly2never
        4
    fly2never  
    OP
       2012 年 4 月 11 日
    @ultragtx 全部用属性而不用成员变量的原因我觉得是通过强制规范禁止使用成员变量,以免内存问题.取消了成员变量就不可能出现成员变量被赋值的情况,全部都通过属性来处理.这样对团队开发很有意义.当然如果一个人开发,能保证setter的时候全部用属性,不用成员变量也没大问题.
    ultragtx
        5
    ultragtx  
       2012 年 4 月 11 日
    @fly2never 赋值取值一般人难道还分不清么 这种加self方法基本能把step into功能废掉了 如果你们团队不进行这种调试那就当我没说
    fly2never
        6
    fly2never  
    OP
       2012 年 4 月 12 日
    @ultragtx 嗯,想了下这样确实不好.class类中如果要使用,还是推荐instance variable.
    原文中有描述:
    "NEVER allow your instance variable names to be confused with property names, or with data member names. ALWAYS end your instance variable names with an underscore. UNLESS you are subclassing a 3rd-party class which already has a data member of the same name, in which case pick another non-similar name or add another underscore and put a comment in as to why you did this.

    Use “@synthesize name = name_;” in your implementation, instead of just “@synthesize name;”

    WHY?
    Even within the class implementation you have to have a very good reason to access a data member directly, instead preferring property accessors."
    fly2never
        7
    fly2never  
    OP
       2012 年 4 月 12 日
    @ultragtx 这里的"NEVER access your own data members directly" 应该是指子类吧.
    其他类调用,默认protect会禁止使用,只有子类才有可能直接使用.
    lasse
        8
    lasse  
       2012 年 4 月 13 日
    fly2never
        9
    fly2never  
    OP
       2012 年 5 月 22 日   1
    edison0951
        10
    edison0951  
       2012 年 5 月 28 日
    @fly2never,那你觉得不要成员变量,全用属性来控制的缺点在哪?
    gonefish
        11
    gonefish  
       2012 年 5 月 28 日
    有很多个人习惯
    关于     帮助文档     自助推广系统     博客     API   /span>   FAQ     Solana     922 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 48ms UTC 19:31 PVG 03:31 LAX 12:31 JFK 15:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86