iOS:Objective-C创建类属性错误:使用未声明的标识符 [英] iOS: Objective-C creating class property error: Use of undeclared identifier
本文介绍了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;
}
这篇关于iOS:Objective-C创建类属性错误:使用未声明的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文