-1

Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0
Objective-C Properties with or without instance variables

What is the difference between the following two pieces of code? Both are compilable and I don't know which is "correct".

@interface JTPlayer : NSObject {

    NSString *userId;
    NSString *name; 
    BOOL inBattle;
}

@property (nonatomic, copy) NSString *userId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) BOOL inBattle;

@end

and

@interface JTPlayer : NSObject

@property (nonatomic, copy) NSString *userId;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) BOOL inBattle;

@end
Community
  • 1
  • 1
Serge
  • 2,031
  • 3
  • 33
  • 56
  • Damned, I typed in an answer and then noticed it's a duplicate. @Sergio, please do a little effort sometimes: accept answers and use the search tool... – Kheldar Sep 02 '11 at 20:30

3 Answers3

1

One is the previous version of declaring properties. As you can see, you needed to declare variables by hand, and then declare properties applied to those variables.

The second is the newer version that manages the rest for you, declaration of variables and correspondences with properties.

Both blocks are correct, but the first one requires more keystrokes. This code (both versions) is supposed to belong to a header file, accompanied with a source file with .m extension that contains the implementation. This implementation will contain @synthesize instructions, that generate the getter and setter methods for you.

For more information, you really should read the Apple Guide to Objective-C. Also check out http://www.raywenderlich.com .

progrmr
  • 75,956
  • 16
  • 112
  • 147
Kheldar
  • 5,361
  • 3
  • 34
  • 63
0

The current version of the Objective-C runtime does not require you to specify you instance variables for properties. @synthesize will add them for you automatically.

Christopher A
  • 2,961
  • 1
  • 22
  • 23
0

Check out this article that I put up awhile ago. It explains about instance variables and properties.

Objective-C Properties with or without instance variables

Community
  • 1
  • 1
5StringRyan
  • 3,604
  • 5
  • 46
  • 69