Red Mak
Red Mak

Reputation: 676

Objective-c coding

I'm really confused about using proprieties. if i declare this

@property (nonatomic, strong) NSString* aString;

what is the difference between this

1.@synthesize aString = _aString

and

2.@synthesize aString;

if i want to use it, what is the difference between:

3. anOtherString = aString;

and

4. anOtherString = self.aString;

and

5. anOtherString = _aString;

I know that the _aString is the ivar, but the problem is the combination between 1,2,3,4,5.

for example, if i use 2 and 4, i'm i passing a reference to anOtherString or a copy of it ? I usually use 2 and 4 is that the best choice for passing a reference ?

Upvotes: 0

Views: 121

Answers (2)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

Your answer are here :

1.@synthesize aString = _aString

Ans: Your property name is aString but you are making an alias _aString. Now you can access aString, your accessors can be used by _aString only. From Xcode4.4 onwards this statement comes by default.

2.@synthesize aString;

Ans: This is the basic style of Objective-c synthesizing a property. From Xcode4.4 onwards this statement overrides the above one.

3.anOtherString = aString;

You are accessing the property by the reference variable/alias.

4.anOtherString = self.aString;

You are using accessor to get its value.

5.anOtherString = _aString;

You are accessing the property itself.

I usually use 2 and 4 is that the best choice for passing a reference ?

Ans : This is good way to do, as self. makes your class KVC compliant.

Upvotes: 1

groomsy
groomsy

Reputation: 4955

When you use your @synthesize declaration, this generates getters/setters for you. When you use the @synthesize declaration @synthesize aString = _aString you are making it so that your getter/setter methods will use setAString and aString. You are creating a private variable called _aString. This means that inside your class, you can get this variable by calling

NSString *tempString = _aString;

or

NSString *tempString = self.aString;

The latter uses the generated getter method to retrieve aString.

You can also set your variable using the following methods:

_aString = @"";

or

self.aString = @"";

The latter uses the generated setter method. This is very important, because if you have declared your property as retain (now strong) or copy, then your setter method performs some logic that you may not be aware of (please review strong/copy/weak properties). Essentially, if you have a strong property and you use the generated setter, your code releases it's previous reference on that variable and creates a strong reference to what you're assigning to your variable.

I personally try to always use the getter/setter approach unless it's a weak reference (then it really doesn't matter). I usually always use the @synthesize aString = _aString approach as well, so that I can name method parameters aString if I choose. It helps avoid naming conflicts.

Please let me know if I can clarify in any way.

Upvotes: 1

Related Questions