5
NSNumber * latitude =  [NSNumber numberWithDouble:[[cityDictionary valueForKeyPath:@"coordinates.latitude"]doubleValue]];

      NSNumber * longitude =  [NSNumber numberWithDouble:[[cityDictionary valueForKeyPath:@"coordinates.longitude"]doubleValue]];


    CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

我在上面的第 3 行收到以下错误:

Sending 'NSNumber *__strong' to parameter of incompatible type 'CLLocationDegrees' (aka 'double')

我知道这是因为我试图将 NSNumber 传递到它期望双倍的地方。但是由于ARC,铸造不起作用?

4

2 回答 2

3

调用[cityDictionary valueForKeyPath:@"coordinates.latitude"]已经给了你一个NSNumber对象。为什么将其转换为 double 然后创建一个新的NSNumber

你可以这样做:

NSNumber *latitude = [cityDictionary valueForKeyPath:@"coordinates.latitude"];
NSNumber *longitude = [cityDictionary valueForKeyPath:@"coordinates.longitude"];
CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];

如果事实证明这[cityDictionary valueForKeyPath:@"coordinates.latitude"]实际上是返回一个NSString而不是一个NSNumber,那么这样做:

CLLocationDegrees latitude = [[cityDictionary valueForKeyPath:@"coordinates.latitude"] doubleValue];
CLLocationDegrees longitude = [[cityDictionary valueForKeyPath:@"coordinates.longitude"] doubleValue];
CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
于 2012-11-17T21:44:30.147 回答
2

您将 NSNumber 类型发送到双精度参数。您可以考虑将其更改为CLLocationDegree's 或double,但如果您在其他地方使用它或将其与核心数据一起存储,我会将其保留为NSNumber.

CLLocation *listingLocation = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
于 2012-11-17T18:04:41.933 回答