初始化一个只读属性

来源:互联网 发布:淘宝贾真 编辑:程序博客网 时间:2024/05/01 21:59

Either assign to the instance variable directly (don't forget to add a retain orcopy if you need it) or redeclare the property in a private class extension. Like this:

In your .h file:

@property (readonly, copy) NSString *firstName;

In your .m file:

@interface MyClass ()// Redeclare property as readwrite@property (readwrite, copy) NSString *firstName;@end@implementation MyClass@synthesize firstName;...
Now you can use the synthesized setter in your implementation but the class interface still shows the property as readonly. Note that other classes that import your .h file can still call-[MyClass setFirstName:] but they won't know that it exists and will get a compiler warning


another

In your .h file:

@property (readonly, copy)NSString*firstName;

@interface MyClass ()
@synthesize firstName  = _firstName;
@end
only can you use _fisrtName to init