更新时间:2023-01-08 13:25:43
您可以将变量设为公开,或者将其变成属性。例如,将其公开:
You can either make the variable public, or make it into a property. For example, to make it public:
@interface Class1
{
@public
int var;
}
// methods...
@end
// Inside a Class2 method:
Class1 *obj = ...;
obj->var = 3;
要使其成为属性:
@interface Class1
{
int var; // @protected by default
}
@property (readwrite, nonatomic) int var;
// methods...
@end
@implementation Class1
@synthesize var;
...
@end
// Inside a Class2 method:
Class1 *obj = ...;
obj.var = 3; // implicitly calls [obj setVar:3]
int x = obj.var; // implicitly calls x = [obj var];