In my program, I am assigning a value to a property in the init method. I later use this property in response to an event.
Given a property named Object; why does the synthesized setter [self setObject:obj] work, but Object = obj give an Invalid Selector exception? Is it because the mutator adds one to the reference count on obj?
Edit: Here is more code, to give context. When I say “work” above, I mean run without errors.
Here is the initialization of the object(A View Controller) which has the property in it:
Note: All properties are declared as (nonatomic, retain).
@synthesize _Kiosk;
....
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andKiosk: (Kiosk*) kiosk
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self set_Kiosk:kiosk]; //This work witout error. _kiosk = kiosk Fails at point below.
}
return self;
}
....
- (IBAction) ActionPressed:(id)sender
{
[_CompanyName setText: [_Kiosk _CompanyName]]; //Failure happens here
}
And here is the method call that initializes this View Controller:
@synthesize _Kiosk;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarHidden:true withAnimation:UIStatusBarAnimationFade];
_Kiosk = [[Kiosk alloc] init];
self.window.rootViewController = [_KioskViewController initWithNibName:@"iPadKioskView" bundle:nil andKiosk: _Kiosk];
[_Kiosk release];
[self.window makeKeyAndVisible];
return YES;
}
@propertybasically just declares two methods:setFoo:andfoo.@synthesizewill create the method bodies for those methods, which will correctly retain the new value and release the old one.Assigning directly to the instance variable, however, doesn’t call those accessor methods. It just makes the pointer point to a new object. If you’re going to manually set the instance variable, then you need to make sure to correctly retain new value and release the old one.
Note that
[self setFoo:someFoo]andself.foo = someFoowill both call the accessor method, whilefoo = someFoojust sets the instance variable directly, bypassing your setter method.