iOS ARC下命名规则
2013-12-28 15:26
232 查看
当我在ARC模式下写以下代码的时候,编译器报错
@interface ViewController : UIViewController {
NSString *newTitle;
}
@property (strong, nonatomic) NSString *newTitle;
.m
@synthesize newTitle;
这是因为在高版本编译器ARC模式下,这种命名规范是不合理的,可以查看苹果官网的内存管理方面的文档中有说明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的时候会生成getter和setter方法,如果有new打头的属性的时候,在生成getter就会调用newTitle方法,编译器认为这是生成
新的对象,而不是get原有的属性,所以就提示错误信息。
解决办法:
1。new前加上别的字符例如theNewTitle
2。重写getter方法
3。第三种是可以new开头,但是要告诉编译器不是new个新对象
#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
4。这种也可以啊
苹果已经有文档Transitioning
to ARC Release Notes说明了开发者在命名的时候避免以 new 和 copy 开头
newButton
newLabel
newTitle
_newButton
mewLabel
neueTitle
#arc #auto-synthesized #xcode-4.6.1
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;
这是因为在高版本编译器ARC模式下,这种命名规范是不合理的,可以查看苹果官网的内存管理方面的文档中有说明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的时候会生成getter和setter方法,如果有new打头的属性的时候,在生成getter就会调用newTitle方法,编译器认为这是生成
新的对象,而不是get原有的属性,所以就提示错误信息。
解决办法:
1。new前加上别的字符例如theNewTitle
@property (strong, nonatomic) NSString *theNewTitle;
2。重写getter方法
@property (strong, nonatomic, getter=theNewTitle) NSString *newTitle;
3。第三种是可以new开头,但是要告诉编译器不是new个新对象
#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
苹果已经有文档Transitioning
to ARC Release Notes说明了开发者在命名的时候避免以 new 和 copy 开头
Unacceptable Object Names
newButtonnewLabel
newTitle
Acceptable Object Names
_newButtonmewLabel
neueTitle
#arc #auto-synthesized #xcode-4.6.1
相关文章推荐
- iOS ARC下命名规则
- iOS ARC下命名规则
- iOS ARC下命名规则
- Cisco IOS 命名规则
- 常用的CSS命名规则
- C语言变量名的命名规则
- CSS样式命名规则(仅供参考)
- Python 命名空间和LEGB规则
- 惠普在笔记本新品的2007新的命名规则
- PHP命名空间规则解析及高级功能(1)
- ARM体系结构的命名规则
- 码农命名的基本规则
- Python命名规则
- 命名规则的力量
- ios项目icon和default图片命名规则
- C#学习之路,学习笔记 2.6 变量的命名规则、常量 及 关键字
- Java 命名规则
- python 学习记录(5)-变量、模块名的命名规则及random模块使用
- css笔记1: html页面的CSS、DIV命名规则
- CSS命名规则