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



在我的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",
        }
    ]
}

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

相关内容

最新更新