-1

I am having problems trying to pass a double value number to another viewController.

This is the NSNumber declaration on the receiving viewController:

@property (nonatomic, assign) NSNumber *lat;

And this is how am I getting the value from the source viewController:

double lat1 = [[[categorias objectAtIndex:i]objectForKey:@"latitud"] doubleValue];
double lon1 = [[[categorias objectAtIndex:i]objectForKey:@"longitud"] doubleValue];
annotation3.lat = [NSNumber numberWithDouble:lat1];
annotation3.lon = [NSNumber numberWithDouble:lon1];
NSLog(@"ANNOTATION LAT %@",annotation3.lat);//Log OK
NSLog(@"ANNOTAION LON %@",annotation3.lon);//Log OK

And then on another method, where I try to pass the value lat to the other viewController:

myAnnotation *ann = (myAnnotation *)view.annotation;
detailViewController.lat = ann.lat;

How should I format 'ann.lat'? I have tried

ann.lat

[ann.lat doubleValue]

[NSNumber numberWithDouble:ann.lat]

And always getting error.

mvasco
  • 4,965
  • 7
  • 59
  • 120

2 Answers2

1

Since the detailViewController.lat is an NSNumber, you need to assign it an NSNumber. Since it is the same in the source view controller, all you need to do is a simple assign, the way that you did in your example:

detailViewController.lat = ann.lat;

The other two choices would be useful in these situations:

  • If the receiving view controller has a double and the source view controller has an NSNumber, you use [ann.lat doubleValue]
  • If the receiving view controller has an NSNumber and the source view controller has a double, you use [NSNumber numberWithDouble:ann.lat]

Logging this NSLog(@"ANN.LAT VALUE = %@", ann.lat); the app crashes on this line

This means that ann.lat is not an NSNumber, so the second situation described above applies.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • if I use detailViewController.lat = [NSNumber numberWithDouble:ann.lat ]; an error is thrown: Sending NSNumber to parameter of incompatible type double – mvasco Mar 24 '14 at 00:49
  • 1
    @mvasco Then it's not declared as `NSNumber` in the destination controller: the error says that in the destination controller `lat` is of type `double`, not `NSNumber` – Sergey Kalinichenko Mar 24 '14 at 00:53
  • it is declared as NSNumber: @property (nonatomic, assign) NSNumber *lat; – mvasco Mar 24 '14 at 01:11
  • @mvasco I see, so it complains about the NSNumber's numberWithDouble then. A straight assignment should work, because the compiler thinks that ann.lat is an NSNumber, too. – Sergey Kalinichenko Mar 24 '14 at 01:18
1

As you are declaring your property as (assign) in your annotation class and I assume that its initial value was assigned from a local variable in a method in that class there is a very good chance that once your detailViewController tries to access the property it has been released because assigning the value to the property doesn't increase the object's reference count. This will result in an access violation exception. Change your property definition in your annotation to

@property  (strong,nonatomic) NSNumber *lat;

and see if that helps

Paulw11
  • 108,386
  • 14
  • 159
  • 186