I have created a class as follows
@interface sampleClass: UIViewController
{
NSString *currentLocation;
}
@property (nonatomic, copy) NSString *currentLocation;
So, whenever the current GPS changes, a function will be called as follows:
-(void)newLocationUpdate:(NSString *)text {
NSString *temp = [[NSString alloc] initWithString:text];
currentGPS = temp;
[temp release];
}
I am wondering if I am doing it right? Thanks.
Ignoring the
currentLocation/currentGPSconfusion — no, that’s still not quite right.You don’t show the the setter for
currentLocation. I’ll assume it’s a@synthesized property. If you just writecurrentLocation = something, you’re not invoking the property setter; you’re just setting the instance variable. This means that after you release the object in the very next line, your instance variable is probably pointing to a deallocated object.The correct way to write it (again, assuming you have a synthesized accessor) would be:
This invokes the property accessor, which copies the object for you.
If for some reason you needed to access the instance variable directly (like if this were the setter method for
currentLocation), you would write: