4

Possible Duplicate:
Properties and Instance Variables in Objective-C 2.0

I'm confused by these two code segments:

First:

//.h
@interface Student : NSObject {

}
    @property (nonautomic, copy) NSString *name;
    @property (nonautomic, retain) NSNumber *age;
@end

//.m
@implementation Student
    @synthesize name;
    @synthesize age;
@end

Second:

//.h
@interface Student : NSObject {
    NSString *name;   // <<============ difference
    NSNumber *age;    // <<============ difference
}
    @property (nonautomic, copy) NSString *name;
    @property (nonautomic, retain) NSNumber *age;
@end

//.m
@implementation Student
    @synthesize name;
    @synthesize age;
@end

Both of these can work. So is it necessary to declare variables in the {}?

Community
  • 1
  • 1
理想评论学派
  • 1,001
  • 4
  • 12
  • 21
  • Your properties should mark as `nonatomic`, not `nonaUtomic` – beryllium Nov 30 '11 at 17:35
  • @5StringRyan that question requires you understand there are multiple versions of the runtime, this is coming from a different level of knowledge. – Joshua Weinberg Nov 30 '11 at 18:21
  • 1
    @JoshuaWeinberg - I don't understand, he asked "So is it need to declare variables in the {}," and the creator of the SO post I pointed to stated, "Is this still valid?" (referring to creating properties without ivars). Also, the answer that got accepted on that post is very similar to what you answered. – 5StringRyan Nov 30 '11 at 18:26
  • Oh, I understand and agree the answers are the same, but the questions are rather different :) – Joshua Weinberg Nov 30 '11 at 18:42

3 Answers3

10

Starting with the modern runtime (x86_64 and ARM6...and iOS Simulator) you no longer need to declare synthesized ivars. In the first example @synthesize is adding the instance variable for you.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
0

Agree with @Joshua. I too was confused with this in the beginning. It's basically old convention vs new convention after the runtime updates. I think Apple realized that declaring ivars was redundant when you're gonna declare @property, so why not let the @synthesize take care of it when it creates the setters and getters. One less statement for us to write, yay!

(Some of these convention changes were explained in one of the earlier WWDC videos... i think)

HM1
  • 1,684
  • 2
  • 18
  • 25
0

The Objective-C Programming Language: Property Implementation Directives

There are differences in the behavior of accessor synthesis that depend on the runtime (see also “Runtime Difference”):

  • For the legacy runtimes, instance variables must already be declared in the @interface block of the current class. If an instance variable of the same name as the property exists, and if its type is compatible with the property’s type, it is used—otherwise, you get a compiler error.

  • For the modern runtimes (see “Runtime Versions and Platforms” in Objective-C Runtime Programming Guide), instance variables are synthesized as needed. If an instance variable of the same name already exists, it is used.

Nate
  • 12,963
  • 4
  • 59
  • 80