3

Here are 2 methods to assign property in Objective-C :

METHOD 1

// in header
@interface Book : NSObject {
    NSString *_title;
}

@property (nonatomic, retain) NSString *title;

// in implementation
@implementation Book
@synthesize title = _title;

METHOD 2

// in header
@interface Book : NSObject {
    NSString *title;
}

@property (nonatomic, retain) NSString *title;

// in implementation
@implementation Book
@synthesize title;

What are the difference? I use Method 1 recently, as more tutorials recommend Method 1, but nobody explains why.

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • 1
    Are you asking about assign vs retain? Or the name of the instance variable being different from the property? There are two differences here. – Firoze Lafeer Dec 30 '11 at 03:17
  • Off the track:Can anybody explain me what is the use of using `@synthesize title = _title`.Or what is the use and when do we use it?Basically what is this doing internally? – Rahul Singh Aug 22 '12 at 12:18

2 Answers2

6

The difference is the names. In #2 the property and instance field have the same name. In #1 they have different names.

The advantage to #1 is that it's difficult to accidentally reference the property when you mean the instance field or vice-versa. Referencing the wrong one can lead to having a object retained twice or not retained at all.

The advantage to #2 is that it's marginally simpler, and it works fine if you're careful and a bit formal in your use of things.

[And, I see, one flavor specifies assign and the other retain, which is a whole different lecture. You'd not normally use assign with an object pointer.]

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

Firstly, I recommend you use copy instead of retain(and assign) for NSString type of instance. If it's Mutable, then it gets copied; If not, then it just gets retained.
Maybe you'll like THIS DISCUSSION.


And for you question, the difference is that first one use the same name and the second one use the different name for iVar & property.
Actually, you have a METHOD 3 to use:

// in header
@interface Book : NSObject {
}

@property (nonatomic, copy) NSString *title;

// in implementation
@implementation Book
@synthesize title;

For @synthesize to work in the legacy runtime, you must either provide an instance variable with the same name and compatible type of the property or specify another existing instance variable in the @synthesize statement. With the modern runtime, if you do not provide an instance variable, the compiler adds one for you. For example, given the following class declaration and implementation.

Here is a sample code of official doc, you can make it clear( it includes difference between your METHOD 1 & METHOD 2):

@interface MyClass : NSObject {
    float sameName;
    float otherName;
}
@property float sameName;
@property float differentName;
@property float noDeclaredIvar;
@end

@implementation MyClass
@synthesize sameName;
@synthesize differentName=otherName;
@synthesize noDeclaredIvar;
@end

The compiler for the legacy runtime would generate an error at @synthesize noDeclaredIvar; whereas the compiler for the modern runtime would add an instance variable to represent noDeclaredIvar.

Note: iPhone applications and 64-bit programs on Mac OS X v10.5 and later use the modern version of the runtime. Other programs (32-bit programs on Mac OS X desktop) use the legacy version of the runtime. You can refer it HERE).

However, I suggest to use METHOD 1 or METHOD 3. As you can just use self.title in code, the property will help you manage the alloc & release. If you use METHOD 2, you may mix title with self.title(but _title is more clear, uh?). :)

Community
  • 1
  • 1
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • Thanks for the detailed answer. Regarding your answer, is **modern runtime** mean LLVM compiler ? – Raptor Dec 30 '11 at 06:29
  • 2
    @ShivanRaptor you're welcome, more detail you can refer it [HERE](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html). And for `Runtime Versions and Platforms`, you can refer it [HERE](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html#//apple_ref/doc/uid/TP40008048-CH106). :) – Kjuly Dec 30 '11 at 07:33
  • 1
    Nice links. To conclude, iPhone apps & 64-bit apps are using modern runtime; 32-bit apps are using legacy runtime. – Raptor Dec 30 '11 at 08:58
  • NSString is one of the object types where it's least useful to use `copy`, since it's invariant. `copy` only takes additional time and space. – Hot Licks Dec 30 '11 at 12:38
  • @HotLicks mum, that's interesting, thank you, I'll take a look at it. :) – Kjuly Dec 30 '11 at 13:27
  • Hi @HotLicks, it's right that `"copy only takes additional time and space"`, but I think may be you'll like [THIS QUESTION](http://stackoverflow.com/questions/387959/nsstring-property-copy-or-retain). :) – Kjuly Dec 30 '11 at 14:06