objective c - How to use properties in Obj C -
i trying learn how use properties in ios programming. want check people here if got right?
say have property
@interface person : nsobject @property nsstring *firstname; @end in implementation
@implementation xyzperson @synthesize firstname; ... @end by this,
a) instance variable named: firstname created
b) whenever want use property inside class, call self.firstname (or setter/getter)
c) can initialize property in init method this:
-(id) init { ... self.firstname=@"sometext"; ... } i believe points mentioned above, correct, right?
what pretty correct although missing few things @property declaration. need @ least property attribute strong or copy or assign. strings might mutable, use copy ensure string can't modified under us. @property (copy) nsstring *firstname. see answer more details this.
most people use nonatomic improve performance disabling thread synchronization in generated getters/setters. see answer more information.
some people recommend against using property accessors in init method (preferring direct ivar access) because subclasses might have overridden setter/getter , might not work correctly until object initialized. practically speaking need worry lot of people ignore advice.
with latest version of xcode don't have add @synthesize manually - underscore-prefixed instance variable (_firstname in example) synthesized automatically.
Comments
Post a Comment