iOS ARC下命名规则
當(dāng)我在ARC模式下寫以下代碼的時(shí)候,編譯器報(bào)錯(cuò)
Semantic
Issue: Property's synthesized getter follows Cocoa naming convention for returning 'owned' objects
@interface ViewController : UIViewController {
NSString *newTitle;
}
@property (strong, nonatomic) NSString *newTitle;
.m
@synthesize newTitle;
這是因?yàn)樵诟甙姹揪幾g器ARC模式下,這種命名規(guī)范是不合理的,可以查看蘋果官網(wǎng)的內(nèi)存管理方面的文檔中有說明the
memory management rules
You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”,
“copy”, or “mutableCopy”.
前面帶有 new 的屬性在@synthesize的時(shí)候會(huì)生成getter和setter方法,如果有new打頭的屬性的時(shí)候,在生成getter就會(huì)調(diào)用newTitle方法,編譯器認(rèn)為這是生成
新的對(duì)象,而不是get原有的屬性,所以就提示錯(cuò)誤信息。
解決辦法:
1。new前加上別的字符例如theNewTitle
@property (strong, nonatomic) NSString *theNewTitle;
2。重寫getter方法
@property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;
3。第三種是可以new開頭,但是要告訴編譯器不是new個(gè)新對(duì)象
#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
4。這種也可以啊
@synthesize newTitle = _newTitle; // Use instance variable _newTitle for storage
蘋果已經(jīng)有文檔Transitioning
to ARC Release Notes說明了開發(fā)者在命名的時(shí)候避免以 new 和 copy 開頭
Unacceptable Object Names
- newButton
- newLabel
- newTitle
Acceptable Object Names
- _newButton
- mewLabel
- neueTitle
#arc #auto-synthesized #xcode-4.6.1
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
總結(jié)
以上是生活随笔為你收集整理的iOS ARC下命名规则的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农情定点监测系统-一款农田智能化管理作物
- 下一篇: 1-25kg金属粉末除尘自动定量包装机设