English 中文(简体)
目标-C 编码准则
原标题:Objective-C coding guidelines

因此,在指导方针中,它说:

对于仅对iOS运行的代码,最好使用自动合成实例变量。

当合成实例变量时, 请使用 @ synthesize var = var_; 因为这样可以防止不小心调用 var = sabel; 当 self. var = label; 时使用 。

 // Header file
@interface Foo : NSObject
// A guy walks into a bar.
@property(nonatomic, copy) NSString *bar;
@end

// Implementation file
@interface Foo ()
@property(nonatomic, retain) NSArray *baz;
@end

@implementation Foo
@synthesize bar = bar_;
@synthesize baz = baz_;
@end

问题是,这是否也只适用于公共变量或私人变量?这在文件上并不十分清楚,但希望对为什么“如果”只用于公共或私人?我认为,它只是所有公共/私人变量都有意义,这样你就不会搞乱伊瓦尔并使用财产。

最佳回答

我不认为这特别关系到所涉变量是公共的还是私人的。 以不同名称合成的做法在直接访问变量而不是使用生成的存取器方法时就明确了。

或许在您询问的问题背后有一个不同的问题 : “ 我一般应该通过存取器还是直接访问私人的静脉? ” 我认为,最熟练的iOS deds 倾向于使用存取器,除非有某些特别的原因(表现、避免KVO等副作用 ) 。 这样做更适合未来,并允许在基本执行中保持灵活性。 以非常小的方式,您重新编码为接口而不是执行。

也值得指出的是,Clang的默认行为将在未来发生变化,从而将支持属性的ivars合成为 默认 。 显然,权力(即考虑强调ivars是一种最佳做法) 。

问题回答

我敢肯定,其中很多归结于个人偏好, 因此,这是我的, 因为它们的价值:

  • I like to distinguish between public properties and "private" instance vars.
  • Properties are always accessed through their accessors, except for initialization (and within a manually created accessor method, for obvious reasons). Hence, the underscore in the backing ivar is useful, and not really an issue in my daily use of the properties.
  • Instance vars are used to hold state that is used internally in the methods, but not (directly) by other classes.
  • I have become very fond of declaring my instance variables in the .m file. Nice, clean and easy (no switching back and forth between .h and .m to declare ivars).
  • I find that this distinction helps me clear my mind and determine if a property is something outside agents should get and/or set directly (a property in .h), or if it is really just a help to get my method implementations to work (an ivar in .m).

我同意保罗的观点 一致性是你的朋友 但对我来说 区别也是朋友





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签