iOS 本地声明隐藏实例变量 + NSCFString objectForKeyedSubscript 无法识别的选择器发送到实例

标签 ios objective-c maps

在我的 iOS 项目中,我收到此警告:

Local declaration of data hides instance variable.

当我运行它时,它崩溃并给出了这个错误:

NSCFString objectForKeyedSubscript unrecognized selector sent to instance.

我想在 map 上显示来自外部 JSON 文件的标记,下面是我的代码:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    [self.mapView setShowsUserLocation:YES];
    MKCoordinateRegion myRegion;
    CLLocationCoordinate2D center;
    center.longitude = 55.130761;
    center.latitude = 25.062718;
    //SPAN
    MKCoordinateSpan span;
    span.longitudeDelta = 0.40f;
    span.latitudeDelta = 0.40f;
    myRegion.span = span;
    myRegion.center = center;
    [self.mapView setRegion:myRegion animated:YES];
    //Loading Data from website
    NSURL *url = [NSURL URLWithString:@"http://example.com/api.php?user_id=2"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    // parse the JSON into a NSArray
    NSError *error;
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    if (error != nil)
    {
        ////
    }
    // a few variables to be used as we iterate through the array of results
    CLLocationCoordinate2D location;                         // coordinates of the annotation
    NSMutableArray *newAnnotations = [NSMutableArray array];
    MKPointAnnotation *newAnnotation;
    // iterate through the array, adding an annotation to our our array of new annotations
    for (NSDictionary *dictionary in array)
    {
        // retrieve latitude and longitude from the dictionary entry
        location.latitude = [dictionary[@"latitude"] doubleValue];
        location.longitude = [dictionary[@"longitude"] doubleValue];
        // create the annotation
        newAnnotation = [[MKPointAnnotation alloc] init];
        newAnnotation.title = dictionary[@"report_no"];
        newAnnotation.coordinate = location;
        [newAnnotations addObject:newAnnotation];
    }
    [self.mapView addAnnotations:newAnnotations];
}

这是我的 JSON 文件的样子:

{
    reports: [
        {
            report_no: "3",
            user_id: "1",
            latitude: "25.085502",
            longitude: "55.158234"
        },
        {
            report_no: "4",
            user_id: "1",
            latitude: "24.931096",
            longitude: "55.018501"
        },
        {
            report_no: "6",
            user_id: "1",
            latitude: "24.993813",
            longitude: "55.094376",

        }
    ]
}

最佳答案

您的 JSON 是一个字典,但您将其视为一个数组。这是您的代码以及后面实际发生的事情:

NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
// array is actually the JSON dictionary containing the key "reports"
if (error != nil)
{
  ////
}
CLLocationCoordinate2D location;
NSMutableArray *newAnnotations = [NSMutableArray array]; 
MKPointAnnotation *newAnnotation;                      
for (NSDictionary *dictionary in array)
{
    // dictionary here is actually the string "reports", because using for () loop on a dictionary iterates on the dictionary keys.
    location.latitude = [dictionary[@"latitude"] doubleValue]; // NSCFString objectForKeyedSubscript unrecognized selector sent to instance
    location.longitude = [dictionary[@"longitude"] doubleValue]; // NSCFString objectForKeyedSubscript unrecognized selector sent to instance

    newAnnotation = [[MKPointAnnotation alloc] init];
    newAnnotation.title = dictionary[@"report_no"]; // NSCFString objectForKeyedSubscript unrecognized selector sent to instance
    newAnnotation.coordinate = location;
    [newAnnotations addObject:newAnnotation];
}
[self.mapView addAnnotations:newAnnotations];

要修复,请将您的 JSON 解码代码更改为

NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (![dictionary isKindOfClass:[NSDictionary class]])
{
    /// handle error.
}
else
{
    NSArray *array = dictionary[@"reports"];
    // ... The rest of your code  
}

关于iOS 本地声明隐藏实例变量 + NSCFString objectForKeyedSubscript 无法识别的选择器发送到实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108742/

相关文章:

c# - 在 Mac OS X 上的 Xamarin Studio 6.1 中使用 System.Security.Cryptography 时出现程序集引用错误 (CS0234)

ios - 互联网重新连接 swift

ios - 如何在按下 tabBar 按钮时调用一个函数,但前提是当前正在按下该 View ?

ios - 如何使用 CMDeviceMotion 检测我的设备移动的速度?

ios - 在 Swift 中使用 commitEditingStyle 动态删除 UITable 部分

Android 谷歌地图 onMapReady store GoogleMap

java - 谷歌地图 android API V2 失败

ios - 使用 Core Data(在 SwiftUI 中)提供的数据并与另一个 View 共享

ios - SceneKit AVPlayer 仅播放音频

javascript - 诺基亚 map - 更改圆坐标