gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:55:56 29 4
gpt4 key购买 nike

在我的 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com