大家好我想将以下plist文件读入内存并对结果进行一些计算,plist文件如下所示,这只是显示结构的一部分:
<dict>
<key>Stations</key>
<array>
<dict>
<key>Name</key>
<string>Hatfield</string>
<key>Coords</key>
<dict>
<key>Lat</key>
<integer>0</integer>
<key>Long</key>
<integer>0</integer>
</dict>
<key>Routes</key>
<array>
<dict>
<key>Name</key>
<string>H1 Hatfield - Brooklyn</string>
<key>Stops</key>
<array>
<dict>
<key>Name</key>
<string>Burnett St & Festival St</string>
<key>Coords</key>
<dict>
<key>Lat</key>
<string>-25.75081</string>
<key>Long</key>
<string>28.23272</string>
</dict>
</dict>
<dict>
<key>Name</key>
<string>University Rd & Lynwood Rd</string>
<key>Coords</key>
<dict>
<key>Lat</key>
<string>-25.75592</string>
<key>Long</key>
<string>28.22517</string>
</dict>
</dict>
<dict>
<key>Name</key>
<string>Roper St & Charles St</string>
<key>Coords</key>
<dict>
<key>Lat</key>
<string>-25.76463</string>
<key>Long</key>
<string>28.22737</string>
</dict>
</dict>
目前我正在尝试使用以下内容阅读它们:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Busses.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
NSMutableArray *arrayOfStationsFromFile = [[[NSMutableArray alloc] initWithObjects:[plistData objectForKey:@"Stations"], nil] retain];
for(int i = 0; i < [arrayOfStationsFromFile count]; i++)
{
NSDictionary *stationWithBusses = (NSDictionary *)[[arrayOfStationsFromFile objectAtIndex:i] retain];
NSMutableArray *routes = [[stationWithBusses objectForKey:@"Routes"] retain];
for(int j = 0; j < [routes count]; j++)
{
NSMutableDictionary *routesData = [routes objectAtIndex:j];
NSString *name = [routesData objectForKey:@"Name"];
NSMutableArray *stops = [routesData objectForKey:@"Stops"];
for(int x = 0; x < [stops count]; x++)
{
NSMutableDictionary *stopsData = [stops objectAtIndex:x];
NSString *subName = [stopsData objectForKey:@"Name"];
NSMutableDictionary *coords = [stopsData objectForKey:@"Coords"];
NSString *latt = [coords objectForKey:@"Lat"];
NSString *longs = [coords objectForKey:@"Long"];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [latt longLongValue];
coordinate.longitude = [longs longLongValue];
[busses addObject:[[Busses alloc] initWithName:name subName:subName coordinate:coordinate]];
}
}
}
但是我在这一行获得了一个SIGABRT
NSMutableArray *routes = [[stationWithBusses objectForKey:@"Routes"] retain];
控制台输出的第一行:
2011-08-30 12:31:57.699 GautrainiPhone[12633:e903] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x5ba6c10
任何想法我做错了什么?
答案 0 :(得分:0)
消息+位置隐含stationWithBusses
是NSArray
(不是NSDictionary
)
答案 1 :(得分:0)
尝试用我的代码替换第一行:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:finalPath];
NSMutableDictionary *plistData = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
NSMutableArray *arrayOfStationsFromFile = [NSMutableArray arrayWithArray:[plistData valueForKey:@"Stations"]];