BNR Item initializer triggering exception


#1

Hi when I followed along the text in the book I got to this initializer:

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t { self = [super init]; if(self) { coordinate = c; [self setTitle:t]; } return self; }

Instead of writing “coordinate = c” I wrote “[self setCoordinate:c]” since this is what we’d grown accustomed to up to this point in the text and because we use the ‘setTitle’ selector in this initializer. When I ran the program it crashed due to the unrecognized selector ‘setCoordinate’.

Experts, please tell me why this is???

Many thanks!


#2

Did you define a (writable) property for the coordinate variable or alternatively did you define the method setCoordinate:(CLLocationCoordinate2D)c that sets the variable?


#3

I had synthesized the properties in .m so that it would create the selector ‘setCoordinate’, however i did declare the property as “readonly” as you suggested. I changed this to “readwrite” and the issue resolved so that I could use ‘[self setCoordinate:c]’. So thank you!

What I don’t understand though is why when it was readonly ‘coordinate = c’ doesn’t also trigger an exception while setCoordinate does. Can you clarify that?

Much appreciated!


#4

[quote]What I don’t understand though is why when it was readonly ‘coordinate = c’ doesn’t also trigger an exception while setCoordinate does. …
[/quote]
That’s because you are bypassing the controls provided by the property interface, and setting the value of the instance variable directly. Inside the implementation file, you are the King.


#5

Not sure I totally understand why that is. But I can work with it. Thank you Ibex!

One last question! I’m using the xcode beta for iOS7 which is what I have on my device. Every single time I run an application it builds successfully but eventually seems to crash or throw off some sort of exception with the ‘return UIApplicationMain’ line highlighted in green with a note “Thread 1 signal:SIGTERM”. Any idea what’s going on here?

[code]#import <UIKit/UIKit.h>

#import “HypnosisterAppDelegate.h”

int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([HypnosisterAppDelegate class]));
}
}
[/code]

Thanks!