Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a boolean type property in objective C class

How to use a boolean property in objective C class, i did it like:

@property (nonatomic, copy) BOOL *locationUseBool;

but it gives error that:

Property with 'copy' attribute must be of object type.

what is the correct way of declaring?

like image 627
Firdous Avatar asked Feb 22 '12 10:02

Firdous


People also ask

How do I set boolean value in Objective-C?

To set a BOOL, you need to wrap the number in a value object with [NSNumber numberWithBool:NO] . But there's little reason to do that. Key-Value Coding is a roundabout way to accomplish this.

What is Boolean in Objective-C?

A boolean is binary value expressed in Objective-C as the primitive type BOOL which can hold either YES or NO (true or false) values. Their primary use in programming is in decision making.

How do you define a property in Objective-C?

@property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.

How do I set up Boolean?

' The ' bool ' type can store only two values: true or false . To create a variable of type bool, do the same thing you did with int or string . First write the type name, ' bool ,' then the variable name and then, probably, the initial value of the variable.


3 Answers

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

In a multi-threaded program, an atomic operation cannot be interrupted partially through, whereas nonatomic operations can.

like image 70
Kamleshwar Avatar answered Oct 23 '22 18:10

Kamleshwar


@property (nonatomic, assign) BOOL locationUseBool;

No asterisk, no copy, no retain.

like image 23
bneely Avatar answered Oct 23 '22 19:10

bneely


This one worked for me.

@property (nonatomic) BOOL locationUseBool;

There is not asterisk * symbol in property declaration. Also, use of 'assign' is optional.

like image 1
Jayprakash Dubey Avatar answered Oct 23 '22 20:10

Jayprakash Dubey