0

I have the following class:

@interface Location : RKObject <NSCoding>{
    NSNumber*  _latitude;
    NSNumber*  _longitude;
    NSNumber*  _route_order;
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, retain) NSNumber* latitude;
@property (nonatomic, retain) NSNumber* longitude;
@property (nonatomic, retain) NSNumber* route_order;

- (id) initWithLatitude:(double) lat  andLongitude:(double) longt;

@end

I would like to bind the coordinate.latitude with _latitude doubleValue and the same thing with longitude. So the value of coordinate.latitude is always set with whatever value is currently at _latitude. Is this possible in objective-C

3 Answers 3

1

There is no "magical" bind in Objective-C what you could do instead of synthesizing properties is writing accessors (which is what @synthesize really do at compilation in fact), something like that :

@interface Location : RKObject <NSCoding> {
    NSNumber*  _route_order;
    CLLocationCoordinate coordinate;
}

@property (nonatomic, assign) NSNumber* latitude;
@property (nonatomic, assign) NSNumber* longitude;
@property (nonatomic, retain) NSNumber* route_order;

- (id) initWithLatitude:(double) lat  andLongitude:(double) longt;

@end

When implementing accessors instead of synthesizing them, the retain / assign / readonly keyword is somewhat informative. In this example I've used assign because your properties won't actually retain references passed.

In Implementation:

@implementation Location

// note the absence of @synthesize there

- (NSNumber *)latitude  { return [NSNumber numberWithDouble:latitude];  }
- (NSNumber *)longitude { return [NSNumber numberWithDouble:longitude]; }
- (void)setLatitude:(NSNumber *)latitude   { coordinate.latitude  = [latitude doubleValue];  }
- (void)setLongitude:(NSNumber *)longitude { coordinate.longitude = [longitude doubleValue]; }

@end

Another alternative, related to "binding" techniques is KVO, but a bit off-topic imho since this a bit too complex for such a simple case.

Sign up to request clarification or add additional context in comments.

2 Comments

but there's no way to set that coordinate.latitude to latitude?
You can by writing the setter and changing readonly by assign.
1

I don't believe there is any way to do what you are asking. But if you misstated your request, and want the latitude property to access the coordinate's latitude (rather than the _latitude ivar), it's a simple matter of writing custom getter and setter methods:

- (NSNumber *)latitude {
    return [NSNumber numberWithDouble:coordinate.latitude];
}

- (void)setLatitude:(NSNumber *)value {
    coordinate.latitude = [value doubleValue];
}

You could then get rid of the _latitude ivar completely.

Comments

0

What it sounds like you want to do is this:

@synthesize latitude = _latitude;

That will make the synthesized setter and getter methods for your latitude property change your _latitude ivar.

2 Comments

I don't think you're getting my question.. I want coordinate.latitude to bind with NSNumber latitude and same goes for longitude
coordinate is a CLCoordinateLocation2D here, FYI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.