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')

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"]返していることが判明した場合は、次のようにします。NSStringNSNumber

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 を double のパラメーターに送信しています。CLLocationDegreeまたはに変更することを検討することもできdoubleますが、他の場所で使用している場合や、コア データと共に保存している場合は、そのままにしておきますNSNumber

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