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

时间:2014-01-14 08:19:21

标签: ios objective-c maps

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

Local declaration of data hides instance variable.

当我运行它时,它会崩溃并发出此错误:

NSCFString objectForKeyedSubscript unrecognized selector sent to instance.

我想在外部 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",

        }
    ]
}

1 个答案:

答案 0 :(得分:1)

您的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  
}