How to store a location (CLLocationCoordinate2D) inside a class w/o leaking memory?

iphone, location, objective-c

Solution

`CLLocationCoordinate2D` is a non-object type. It does not inherit from `NSObject` so it cannot be `retain`ed. So declare your instance variable like so

CLLocationCoordinate2D location;

With the property declaration like:

@property (nonatomic, assign) CLLocationCoordinate2D location;

And then your `setLocation:` method is simply:

- (void)setLocation:(CLLocationCoordinate2D)newLocation {
    location = newLocation;
    // other stuff
}

This `setLocation:` method is also optional since the synthesized property will assign for you. But you seem to want to do some other stuff when a new location is assigned, so this should let you do that.

Problem

I am creating a library which contains an API for setting the current location based off some value collected by the GPS. I want to store this location in my class and later, change the behavior of my library if it is set. What I had in mind was: ``` @interface myLib { @property (nonatomic, retain) CLLocationCoordinate2D *location; } @implementation myLib { @synthesize location = _location; - (void)setLocation:(CLLocationCoordinate2D *)loc { location = loc; } - (void)someFunc { if (location != nil) ... } } ``` However, retain isn't a valid property for a CLLocationCoordinate2D object. So, what is the proper way to save CLLocationCoordinate2D for later use w/o wasting memory?

Original source