语义问题:属性的合成getter遵循Cocoa命名约定来返回'拥有'对象 [英] Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects
问题描述
我目前正在使用iOS 5 SDK尝试开发我的应用。
我正在尝试将NSString作为属性,然后在.m文件中合成它(我之前没有遇到任何问题)。现在,我遇到了这个:语义问题:属性的合成getter遵循Cocoa命名约定返回'拥有'对象。
I'm currently using the iOS 5 SDK trying to develop my app. I'm trying to make an NSString a property, and then to synthesize it in the .m file (I have done this before with no issues). Now, I came across this: "Semantic Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects."
这是我的代码:
.h
This is my code: .h
@interface ViewController : UIViewController {
NSString *newTitle;
}
@property (strong, nonatomic) NSString *newTitle;
.m
@synthesize newTitle;
有没有人知道如何解决这个问题?
谢谢!
Does anyone have a clue how I could fix this? Thanks!!
推荐答案
我的猜测是您使用的编译器版本遵循声明属性的内存管理规则 - 更具体地说,对于声明的属性的访问者:
My guess is that the compiler version you’re using follows the memory management rules for declared properties, too — more specifically, for declared properties’ accessors:
如果使用名称以alloc开头的方法创建对象,则获取对象的所有权, new,copy或mutableCopy。
You take ownership of an object if you create it using a method whose name begins with "alloc", "new", "copy", or "mutableCopy".
名为 newTitle $ c的属性$ c>,当合成时,产生一个名为
-newTitle
的方法,因此出现警告/错误。 -newTitle
应该是 newTitle
属性的getter方法,但命名约定表明名称开头的方法使用 new
返回一个由调用者拥有的对象,这不是getter方法的情况。
A property named newTitle
, when synthesised, yields a method called -newTitle
, hence the warning/error. -newTitle
is supposed to be a getter method for the newTitle
property, however naming conventions state that a method whose name begins with new
returns an object that’s owned by the caller, which is not the case of getter methods.
你可以通过以下方式解决此问题:
You can solve this by:
-
重命名该属性:
Renaming that property:
@property (strong, nonatomic) NSString *theNewTitle;
保留属性名称并指定不以其中一个开头的getter名称特殊方法名称前缀:
Keeping the property name and specifying a getter name that doesn’t begin with one of the special method name prefixes:
@property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;
保留属性名称和getter名称,并告诉编译器,即使getter名称以 new
开头,它属于 none
方法系列,而不是新的
方法系列:
Keeping both the property name and the getter name, and telling the compiler that, even though the getter name starts with new
, it belongs to the none
method family as opposed to the new
method family:
#ifndef __has_attribute
#define __has_attribute(x) 0 // Compatibility with non-clang compilers
#endif
#if __has_attribute(objc_method_family)
#define BV_OBJC_METHOD_FAMILY_NONE __attribute__((objc_method_family(none)))
#else
#define BV_OBJC_METHOD_FAMILY_NONE
#endif
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *newTitle;
- (NSString *)newTitle BV_OBJC_METHOD_FAMILY_NONE;
@end
请注意,即使此解决方案允许您保留 newTitle
作为属性名称和getter名称,有一个名为 -newTitle
的方法,它不返回调用者拥有的对象可以让其他人阅读你的代码会让人感到困惑。
Note that even though this solution allows you to keep newTitle
as both the property name and the getter name, having a method called -newTitle
that doesn’t return an object owned by the caller can be confusing for other people reading your code.
记录,Apple已发布过渡到ARC发行说明,他们声明:
您不能为属性指定一个以
new $开头的名称c $ c>或
复制
。
他们已经收到通知他们的陈述不太准确:罪魁祸首是getter方法名称,而不是属性名称。
They’ve already been notified that their statement is not quite accurate: the culprit is the getter method name, not the property name.
编辑2015年1月17日:我只是注意到了ed a 最近对Clang的提交建议上面的选项3(使用 objc_method_family(none)
),包括fix-it,用于属性名称与特殊方法族前缀之一匹配的一般情况。 Xcode最终可能会包含这一变化。
Edit 17 Jan 2015: I’ve just noticed a recent commit to Clang that suggests option 3 above (using objc_method_family(none)
), including a fix-it, for the general case where a property name matches one of the special method family prefixes. Xcode will likely incorporate this change eventually.
这篇关于语义问题:属性的合成getter遵循Cocoa命名约定来返回'拥有'对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!