gran_profaci
gran_profaci

Reputation: 8461

How can I create a strong Boolean in Objective C

I understand that I cannot have something like :

@property (strong, nonatomic) BOOL didSomethingHappen;

What I am trying to replicate is that in a particular ViewController which segues from a TableViewCell, if a particular action was committed, then set the BOOL didSomethingHappen to True. Now, if you go to the same View Controller but from a different TableViewCell, then didSomethingHappen should be the default value.

I thought what I need is a nice strong Boolean for every individual ViewController object. Can someone tell me just how to achieve this? I'm very new to Objective C.

To Recap.

|    Table Cell 1    | ----------> |    VC with didSomethingHappen     |
|    Table Cell 2    | ----------> |    VC with !didSomethingHappen    |
|    Table Cell 3    | ----------> |    VC with didSomethingHappen     |

Thanks a lot guys.

Upvotes: 0

Views: 1843

Answers (2)

Paulw11
Paulw11

Reputation: 114974

(strong) won't help you here. (strong) is to do with the way in which assignment to a property deals with the reference count. If you assign an object to a (strong) property then the reference count of the assigned object is increased, preventing it from being released while the property holds a reference. A (weak) property won't do this, so the referenced object may be released while the property holds the reference. In this case the property will be set to nil.

As a BOOL is an intrinsic type and not an object, its value is simply assigned to the property, the property does not hold a reference; so you can't make it (strong) or (weak).

Properties exist per object instance. So, you need to ensure that the action for each cell instantiates a new copy of the view controller and assigns bool appropriately before invoking the segue (or as part of prepareForSegue).

Upvotes: 2

sha
sha

Reputation: 17860

You can always wrap BOOL into NSNumber* for example and create a strong pointer to that object. Check for numberWithBool and boolValue functions https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html

Upvotes: 0

Related Questions