从 plist 文件中读取包含多个字典的多个数组,其中包含多个 arryes
大家好,我想将以下 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
知道我做错了什么吗?
Hi guys I want to read the following plist file into memory and do some calculations with the results, the plist file looks as follows this is only part of it to show the structure:
<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>
Currently I am trying to read them using the following:
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]];
}
}
}
But I get a SIGABRT at this line
NSMutableArray *routes = [[stationWithBusses objectForKey:@"Routes"] retain];
First line of console output:
2011-08-30 12:31:57.699 GautrainiPhone[12633:e903] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x5ba6c10
Any ideas what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
message+location 暗示
stationWithBusses
是一个NSArray
(不是NSDictionary
)the message+location implies
stationWithBusses
is anNSArray
(notNSDictionary
)尝试用我的代码替换第一行:
Try to replace first lines with my code: