日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS ARC下命名规则

發(fā)布時(shí)間:2023/12/18 编程问答 45 如意码农
生活随笔 收集整理的這篇文章主要介紹了 iOS ARC下命名规则 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

當(dāng)我在ARC模式下寫(xiě)以下代碼的時(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ī)范是不合理的,可以查看蘋(píng)果官網(wǎng)的內(nèi)存管理方面的文檔中有說(shuō)明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。重寫(xiě)getter方法

@property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;

3。第三種是可以new開(kāi)頭,但是要告訴編譯器不是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

蘋(píng)果已經(jīng)有文檔Transitioning
to ARC Release Notes說(shuō)明了開(kāi)發(fā)者在命名的時(shí)候避免以 new 和 copy 開(kāi)頭

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)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。