views:

1442

answers:

2

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?

Thanks in advance!

A: 

@synthesize will generate the getters and setters for you.

Therefore this is redundant - (void)setLocation:(CLLocationCoordinate2D *)loc { location = loc; }

And technically wrong, because that's "assign" setter and not a "retain" setter.

This is where my guess the leak is happening

CVertex
Well this is a simplified example, hte actual set call also does a quick math operation.What would the matching @property string be?
Henry
+5  A: 

CLLocationCoordinate2D is a non-object type. It does not inherit from NSObject so it cannot be retained. 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.

Squeegy
ah yes, good pt, forget it's not a NSObject - compiler should know this, i wonder if he's getting warnings and wonder what the compiler does here.
CVertex