問題描述
我目前正在使用 iOS 5 SDK 嘗試開發(fā)我的應(yīng)用程序.我正在嘗試使 NSString 成為屬性,然后在 .m 文件中合成它(我之前已經(jīng)這樣做了,沒有任何問題).現(xiàn)在,我遇到了這個問題:語義問題:屬性的合成 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
@interface ViewController : UIViewController {
NSString *newTitle;
}
@property (strong, nonatomic) NSString *newTitle;
.m
@synthesize newTitle;
有人知道我該如何解決這個問題嗎?謝謝!!
Does anyone have a clue how I could fix this? Thanks!!
推薦答案
我猜你使用的編譯器版本如下 內(nèi)存管理規(guī)則 用于聲明的屬性——更具體地說,用于聲明的屬性的訪問器:
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"開頭的方法創(chuàng)建對象,您將獲得該對象的所有權(quán).
You take ownership of an object if you create it using a method whose name begins with "alloc", "new", "copy", or "mutableCopy".
一個名為 newTitle
的屬性在合成時會產(chǎn)生一個名為 -newTitle
的方法,因此會出現(xiàn)警告/錯誤.-newTitle
應(yīng)該是 newTitle
屬性的 getter 方法,但是命名約定規(guī)定名稱以 new
開頭的方法返回一個對象它歸調(diào)用者所有,而 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
與 new
方法族相對的方法族:
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
的方法不會返回調(diào)用者擁有的對象可能會讓其他閱讀您的代碼的人感到困惑.
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 已發(fā)布 Transitioning到 ARC 發(fā)行說明,他們在其中聲明:
您不能為屬性指定以 new
或 copy
開頭的名稱.
You cannot give a property a name that begins with
new
orcopy
.
他們已經(jīng)被告知他們的陳述不太準(zhǔn)確:罪魁禍?zhǔn)资?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 日我剛剛注意到一個 最近對 Clang 的提交建議上面的選項 3(使用 objc_method_family(none)
),包括一個修復(fù)它,用于屬性名稱與特殊方法系列前綴之一匹配的一般情況.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.
這篇關(guān)于語義問題:屬性的合成 getter 遵循 Cocoa 命名約定以返回“擁有"對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!