-2

I was trying to learn how to use properties in iOS programming. I just want to check with people here if what I got is right?

Say I have a property

@interface Person : NSObject

@property NSString *firstName;

@end

in implementation

@implementation XYZPerson
@synthesize firstName;
...
@end

By this,

a) an instance variable named: firstName is created

b) whenever I want to use property inside my class, I call self.firstName (or setter/getter)

c) I can initialize the property in the init method like this:

-(id) init {
 ...
 self.firstName=@"SomeText";
 ... 
 }

I believe the points I mentioned above, are correct, right?

user2054339
  • 393
  • 1
  • 6
  • 20
  • Yes, correct!! For more, refer Apple documentation http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html –  Jul 25 '13 at 11:44
  • One more thing, remember the use of @ in init method for `NSString` –  Jul 25 '13 at 11:47
  • possible duplicate of [Do I use \_property or self.property in implementation](http://stackoverflow.com/questions/12791851/do-i-use-property-or-self-property-in-implementation) – jscs Jul 25 '13 at 19:21

1 Answers1

4

What you say is pretty much correct although you are missing a few things from your @property declaration. You need at the very least a property attribute like strong or copy or assign. For strings which might be mutable, we generally use copy to ensure the string can't be modified from under us. So @property (copy) NSString *firstName. See this answer for more details about this.

Most people use nonatomic as well to improve performance by disabling thread synchronization in the generated getters/setters. See this answer for more information.

Some people recommend against using property accessors in the init method (preferring direct ivar access) because subclasses might have overridden the setter/getter and might not work correctly until the object is fully initialized. Practically speaking you very rarely need to worry about this so a lot of people ignore this advice.

With the latest version of Xcode you also don't have to add the @synthesize manually - an underscore-prefixed instance variable (_firstName in your example) will be synthesized for you automatically.

Community
  • 1
  • 1
Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • 1
    +1 for beating me to it. – Abizern Jul 25 '13 at 11:47
  • Hi Mike, thanks. I just like above approach so if it is ok I think I will use it mostly (I understand your comments about copy etc.). Final things. If I have just a instance variable (no property) -- I don't need to use `self` before it when referring to it right? (Whereas with properties, I must always use `self.propertyName?`) – user2054339 Jul 25 '13 at 11:52
  • If you only have an instance variable, you can't use the dot-property syntax at all. – Mike Weller Jul 25 '13 at 11:53
  • @Mike: Which means I must refer to it directly. e.g., ivarName – user2054339 Jul 25 '13 at 11:57
  • @Mike: How one should decide whether to use instance variables or properties? I think one can always use properties right. – user2054339 Jul 25 '13 at 12:58
  • It's an age-old question: instance var or getter? I would go with using the property syntax unless you have critical performance concerns or some other good reason. – Mike Weller Jul 25 '13 at 13:12
  • @Mike: by getter you mean a property? – user2054339 Jul 25 '13 at 13:34