gpt4 book ai didi

objective-c - 是否需要在@interface 中声明ivars 来匹配属性?

转载 作者:太空狗 更新时间:2023-10-30 03:59:03 25 4
gpt4 key购买 nike

<分区>

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

我对这两个代码段感到困惑:

首先:

//.h
@interface Student : NSObject {

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

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

第二个:

//.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

这两个都可以。那么有必要在{}中声明变量吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com