从 plist 文件中读取包含多个字典的多个数组,其中包含多个 arryes

发布于 2024-12-02 14:07:55 字数 4418 浏览 0 评论 0原文

大家好,我想将以下 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 &amp; 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 &amp; 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 &amp; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

々眼睛长脚气 2024-12-09 14:07:55

message+location 暗示 stationWithBusses 是一个 NSArray (不是 NSDictionary

the message+location implies stationWithBusses is an NSArray (not NSDictionary)

記憶穿過時間隧道 2024-12-09 14:07:55

尝试用我的代码替换第一行:

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"]];

Try to replace first lines with my code:

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"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文