Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to declare ivars in @interface to match properties? [duplicate]

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 {}?

like image 699
理想评论学派 Avatar asked Dec 28 '22 10:12

理想评论学派


1 Answers

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.

like image 134
Joshua Weinberg Avatar answered Jan 13 '23 15:01

Joshua Weinberg