fillky
fillky

Reputation: 581

@property is not set to new value

I have one class as Model - Filter. I need to access this model from my controller. Filter.h

@interface Filter : NSObject

@property (nonatomic, assign) BOOL name;

@end

In my controller:

@implementation NavigationController

@synthesize filter = _filter;

- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
    self.filter.virtualRoomSet = YES; 
    _filter.virtualRoomSet = YES; // I dont know which syntax is better
    }
@end

But it doesnt update virtualRoomSet it is still 'NO'. Where is bug? Thanks

Upvotes: 1

Views: 1305

Answers (2)

commanda
commanda

Reputation: 4881

It looks like you're calling setVirtualShowProperty: before calling navigationController.filter = someFilter;.

The fact that you're setting your boolean value to YES and still seeing it be NO is misleading - it's not really NO, it's just that the object you're setting it on is probably nil.

You should ensure that your navigationController.filter object is indeed not nil when you get to setVirtualShowProperty:.

Upvotes: 0

Cliff
Cliff

Reputation: 11238

Show a little more of your filter implementation. There could be a bunch of things causing a property not to be set. Most notably the fact that it is not in your public interface.

@interface Filter : NSObject

@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;

@end

Putting it there will not make a difference but it should make it more clear the intention of the object. Regarding your controller implementation:

#import "Filter.h"

@implementation NavigationController

@property (nonatomic, retain) Filter *filter;

@end

@implementation NavigationController

@synthesize filter;

//assuming you've properly alloced in init...

-(void) dealloc
{
   [filter release];
   [super dealloc];
}

- (IBAction)setVirtualShowProperty:(UISwitch*)sender {
    self.filter.virtualRoomSet = YES; //better syntax as dealing directly w/ iVars is error prone

}
@end

Using properties instead of accessing iVars directly reduces the amount of memory mgmt code you have to write and provides the ability to replace iVars w/ collaborating objects as necessary. I usually avoid declaring iVars all together since they are now implicitly assumed. That gets you away from the "@synthesize myVar = _myVar;" ugliness.

If you have something like this in your filter:

@interface Filter : NSObject {
    BOOL _virtualRoomSet;
}

@property (nonatomic, assign) BOOL name;
@property (nonatomic, assign) BOOL virtualRoomSet;

@end

And this in your Filter implementation:

@implementation Filter
@synthesize filter;

//other code

@end

...it could explain the bug, which further underscores my point above about avoiding declaring iVars! In other words make sure you are actually setting the thing you want to set. An implicit iVar is now automatically declared for all properties since about iOS SDK 3.0 or probably earlier. Rule of thumb, kill iVars in your code and deal only with properties.

Upvotes: 1

Related Questions