itsame69
itsame69

Reputation: 1560

Objective-C properties on primitive data types

Having something like this

@interface MyClass : UIImageView {
   BOOL autoResize;
}
@property BOOL autoResize;
@end

I create an array of objects like this:

MyClass* o1 = [[MyClass alloc] init];
o1.autoResize = true;
[myArray addObject:o1];

MyClass* o2 = [[MyClass alloc] init];
o2.autoResize = false
[myArray addObject:o2];

The problem is the following: if I now use an iterator to iterate through all objects, myObject.autoResize allways(!) returns false. E.g:

for (MyClass elem in myArray) {
    elem.autoResize ? NSLog(@"true") : NSLog(@"false");
}

would echo "false", "false". I guess I have a vague why this happens (because BOOL is a primitive data type and not an object). But what is the best practice to deal with this issue?

Thanks

Christian

Upvotes: 1

Views: 589

Answers (2)

itsame69
itsame69

Reputation: 1560

Sorry folks! That was a perfect example of why you should revisit your code before posting it here... Actually, what I did was:

MyClass* o1 = [[MyClass alloc] init];
o1.autoResize = true;
[myArray addObject:o1];

myArray = [[NSMutableArray alloc] init];  // OMG! What is this doing here :-(

MyClass* o2 = [[MyClass alloc] init];
o2.autoResize = false
[myArray addObject:o2];

As I had dozends of objects I simply overlooked this line... Thanks @Carl Veazey for the hint.

bye Christian

Upvotes: 0

Ivan Dyachenko
Ivan Dyachenko

Reputation: 1348

Try to add (nonatomic, assign) property modifiers

@property (nonatomic, assign) BOOL autoResize;

How it work you can read here: Declared Properties

Upvotes: 1

Related Questions