iOS:Objective-C创建类属性错误:使用未声明的标识符
问题描述:
我正在尝试根据此示例使用类属性.但出现以下错误:使用未清除的标识符'_myProperty'".
I'm trying to use class property following this example. But I'm getting the following error:"Use of undecleared identifier '_myProperty'".
这是我的实现方式
@interface myClass()
@property (class,strong,nonatomic) NSString *myProperty;
@end
+ (NSString*)myProperty
{
if (!_myProperty) {
}
return [NSString new];
}
为什么我会收到此错误?或您中有人知道解决此问题的方法吗?
Why I'm getting this error? or any of you knows a work around this?
非常感谢您的帮助
答
Class属性不会在Objective-C中进行综合.您必须提供自己的支持变量和自己的getter/setter:
Class properties don't get synthesized in Objective-C. You have to provide your own backing variable and your own getter/setter:
static NSString *_myProperty = nil;
+ (NSString *)myProperty {
if (!_myProperty) {
_myProperty = [NSString new];
}
return _myProperty;
}
+ (void)setMyProperty:(NSString *)myProperty {
_myProperty = myProperty;
}