我有一个按下按钮触发的方法。这是大部分的实施:
[self.placeDictionary setValue:@"166 Bovet Rd" forKey:@"Street"];
[self.placeDictionary setValue:@"San Mateo" forKey:@"City"];
[self.placeDictionary setValue:@"CA" forKey:@"State"];
[self.placeDictionary setValue:@"94402" forKey:@"ZIP"];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
CLLocationCoordinate2D coordinate = location.coordinate;
PFGeoPoint* userLocation = [PFGeoPoint geoPointWithLatitude:coordinate.latitude longitude:coordinate.longitude];
NSLog(@"%f,%f", userLocation.latitude, userLocation.longitude);
} else {
NSLog(@"location error");
return;
}
}];
但是,我收到以下异常:
*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSUnknownKeyException> [<__NSDictionaryI 0x873a3c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Street.
我完全不知道这个例外意味着什么。有人可以帮助我理解为什么会产生这个吗?
答案 0 :(得分:0)
首先,您尝试将对象添加到不可变字典中。从[<__NSDictionaryI 0x873a3c0> setValue:forUndefinedKey:
开始的异常部分给出了__NSDictionaryI
的类名,它是NSDictionary
类集群的不可变成员 - 因此您无法在运行时向其添加任何对象。在调用此代码之前,您需要确保将self.placeDictionary
分配给NSMutableDictionary
实例。
不幸的是,您还使用了错误的方法来添加对象 - 您使用的是setValue:forKey:
而不是setObject:forKey:
。由于此方法是NSKeyValueCoding
非正式协议的一部分,因此您不会在编译时停止这样做。您应该使用setObject:forKey:
这是在NSMutableDictionary
上设置键值对的正确方法。更正第一个问题后,请将setValue:forKey:
来电替换为setObject:forKey:
,例如:
[self.placeDictionary setObject:@"San Mateo" forKey:@"City"];