希望有人可以帮助我解决我一直在努力解决的问题...
使用 MapBox 开发基于地图的应用程序,我想将 NSMutableDictionary 附加到每个地图注释以存储其他数据。我让它工作了,但 XCode 一直在警告我关于我的一些数据/对象类型,所以我检查并整理了它们,现在它坏了。这个想法是,在 ViewDidLoad 上,程序运行一组 plist 字典以正确设置每个注释 - 这仍然可以正常运行,因为我的初始 anno 标记会以正确的设置弹出。但是,我不想每次都返回 plist,而是想将字典附加到每个注释的 userinfo 属性,然后我可以使用它来切换选择数据和其他功能。这是我的代码:
NSDictionary *ExploreSteps = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ExploreSteps" ofType:@"plist"]];
for (NSString *key in [ExploreSteps allKeys])
{
//Loop through keys for each anno
NSDictionary *thisStep = [ExploreSteps objectForKey:key];
NSNumber *annoIndex = [thisStep objectForKey:@"Index"];
NSNumber *isLive = [thisStep valueForKey:@"isLive"];
NSString *annoTitle = [thisStep objectForKey:@"Title"];
NSString *annoText = [thisStep objectForKey:@"Text"];
NSString *imagefile = [thisStep objectForKey:@"Imagefile"];
double longitude = [[thisStep objectForKey:@"Longitude"] doubleValue];
double latitude = [[thisStep objectForKey:@"Latitude"] doubleValue];
NSString *pagefile = [thisStep objectForKey:@"Pagefile"];
NSString *audiofile = [thisStep objectForKey:@"Audiofile"];
CLLocationCoordinate2D annoCoord = CLLocationCoordinate2DMake(latitude, longitude);
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:annoCoord andTitle:annoTitle];
annotation.annotationIcon = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagefile ofType:@"png"]];
annotation.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, @"index", isLive, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];
NSLog(@"Title: %@",[annotation.userInfo objectForKey:@"annoTitle"]);
[mapView addAnnotation:annotation];
}
NSLog 应该吐出 annoTitle 字符串,但它每次都给我一个 null,并且应用程序其余部分的行为也表明存储在字典中的信息根本不是“通过”。
有任何想法吗?提前致谢!
ETA:用于初始化字典的修改代码(并不是说它似乎对问题有任何影响!):
NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:annoIndex, @"index", isLive, @"isLive", annoTitle, @"annoTitle", annoText, @"annoText", imagefile, @"imagefile", pagefile, @"pagefile", audiofile, @"audiofile", nil];
annotation.userInfo = myUserInfo;
NSLog(@"Title: %@",[annotation.userInfo objectForKey:@"annoTitle"]);
NSLog(@"Length: %u",[[annotation.userInfo allKeys] count]);
(标题现在返回“(null)”,而长度返回“1”,如果这有帮助的话......)