views:

288

answers:

2

Hello.

I'm developing an iPhone application and I getting that warning at method:

NSNumber *latitudeValue;
NSNumber *longitudeValue;

[self obtainLatitude:latitudeValue longitude:longitudeValue];

The method is declared as follows:

- (void) obtainLatitude:(NSNumber *)latitudeValue longitude:(NSNumber *)longitudeValue {

    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];

    latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
    longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];

    [f release];
}

As you can see, I'm trying to calculate latitudeValue and longitudeValue calling obtainLatitude:longitude: but I'm doing something wrong.

How can I fix that error?

+2  A: 

You are indeed passing the pointers by value, so when you reassign them, that just takes effect inside thee method. One alternative is to do the following:

- (void) obtainLatitude:(NSNumber **)latitudeValue longitude:(NSNumber **)longitudeValue {

    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];

    *latitudeValue = [f numberFromString:[latitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];
    *longitudeValue = [f numberFromString:[longitude.text stringByReplacingOccurrencesOfString:@"," withString:@"."]];

    [f release];

}

then your call would look like:

NSNumber *latitudeValue;
NSNumber *longitudeValue;

[self obtainLatitude:&latitudeValue longitude:&longitudeValue];
Elfred
Thank you. It works.
VansFannel
+5  A: 

Elfred's answer works, but pass-by-reference for non-NSError** parameters is pretty uncommon. As well, coordinates -- numeric values, in general -- are most typically stored in regular old C types in structures because, comparatively, an NSNumber is quite a bit of overhead (no big deal for a few of 'em, would be a problem if you have a few dozen, hundred, or thousands of coordinates).

Something like:

struct MyLocation {
  CGFloat latitude;
  CGFloat longitude;
};
typedef struct MyLocation MyLocation;

Then:

- (MyLocation) mapCoordinates {
    MyLocation parsedLocation;

    parsedLocation.latitude = ....;
    parsedLocation.longitude = ....;

    return parsedLocation;
}

Something like the above would be more typical in an iPhone/Cocoa program.

As Dave points out, you really don't need to define your own type for this. Use CLLocationCoordinate2D or CLLocation.

bbum
+1 Or use a `CLLocationCoordinate2D`, which is pretty much the same thing (except it uses a double and not a float).
Dave DeLong
I use NSNumber to check if latitude.text is a valid number. If you know another way to check this, I'll use it.
VansFannel
Personally, I'd use just one number formatter to do conversion to NSNumber and then convert that to the scalar type used in CLLocationCoordinate2D.
bbum