ios 如何在目标 C 类中设置布尔类型属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9393114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a boolean type property in objective C class
提问by Firdous
How to use a boolean property in objective C class, i did it like:
如何在目标 C 类中使用布尔属性,我是这样做的:
@property (nonatomic, copy) BOOL *locationUseBool;
but it gives error that:
但它给出了错误:
Property with 'copy' attribute must be of object type.
具有 'copy' 属性的属性必须是对象类型。
what is the correct way of declaring?
什么是正确的声明方式?
回答by Kamleshwar
You can declare this way also.
你也可以这样声明。
@property (assign) BOOL locationUseBool;
Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects
基本上,如果您说非原子,并且使用@synthesize 生成访问器,那么如果多个线程尝试一次更改/读取属性,则可能会发生错误。您可以获得部分写入的值或过度释放/保留的对象
In a multi-threaded program, an atomic operation cannot be interrupted partially through, whereas nonatomic operations can.
在多线程程序中,原子操作不能被部分中断,而非原子操作可以。
回答by bneely
@property (nonatomic, assign) BOOL locationUseBool;
No asterisk, no copy, no retain.
没有星号,没有复制,没有保留。
回答by Jayprakash Dubey
This one worked for me.
这个对我有用。
@property (nonatomic) BOOL locationUseBool;
There is not asterisk * symbol in property declaration. Also, use of 'assign' is optional.
属性声明中没有星号 * 符号。此外,'assign' 的使用是可选的。