EXC_BAD_ACCESS crash: self.x vs _x
The following code does not crash
- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
_heading = newHeading.trueHeading;
NSLog(@"heading - %.2f", newHeading.trueHeading);
//NSLog(@"Updating heading - %f", newHeading.trueHeading);
}
where as this one does
- (void) locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
self.heading = newHeading.trueHeading;
NSLog(@"heading - %.2f", newHeading.trueHeading);
//NSLog(@"Updating heading - %f", newHeading.trueHeading);
}
Can someone explain why this is the case? The property looks like:
@property (nonatomic, assign) fl开发者_运维知识库oat heading;
Also it is never initialized in the init method because I don't want to store a value until it is right. Right now it is:
- (id) initUser
{
return [self init];
}
After you declare a property, you should use @synthesize
to tell the compiler to generate a getter and setter method. By default, these methods are named foo and setFoo, where foo is a name of the variable. Also, by default, the variable represented by the property has the same name as a property, unless you change this with @synthesize foo=_foo to use to a different named instance variable.
精彩评论