0

我有一个 plist,其中包含节点的每个字典信息。每个节点都有经度、纬度和与其他节点的连接。这是 plist 的一小部分。

<array>
    <dict>
        <key>connections</key>
        <array>
            <integer>1</integer>
            <integer>3792</integer>
        </array>
        <key>latitude</key>
        <real>45.43876</real>
        <key>longitude</key>
        <real>12.3213</real>
    </dict>
    <dict>
        <key>connections</key>
        <array>
            <integer>0</integer>
            <integer>3793</integer>
        </array>
        <key>latitude</key>
        <real>45.43887</real>
        <key>longitude</key>
        <real>12.32122</real>
    </dict>

我还有一个名为 IGNode 的类来存储信息,请参见此处的 .m 实现。我认为这里不需要显示标题。

#import <Foundation/Foundation.h>

@interface IGNode : NSObject

@property double lon;
@property double lat;
@property(nonatomic,strong) NSMutableArray *links;


@end

到目前为止,我已经加载了工作的纬度和经度。但是我不知道如何从 plist 中获取连接数组。我查看了很多关于 stackoverflow 的示例,但我无法将它们转化为我必须做的事情。

这就是我到目前为止所拥有的。

for (int i=0; i<plistData.count; i++) {
    NSDictionary *nodeDict = plistData[i];

    IGNode *node = [self.nodes objectAtIndex:i];

    node.lon = [[nodeDict valueForKey:@"longitude"] doubleValue];
    node.lat = [[nodeDict valueForKey:@"latitude"] doubleValue];

    // handle connections
    // ?

}

如何将连接数组存储在 node.links 中?

4

2 回答 2

1

你有没有试过这个......

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Configs" ofType:@"plist"];
NSArray *tmpDicts = [[NSArray alloc] initWithContentsOfFile:plistPath];

然后,您可以枚举 tmpDicts 数组并从每个数组中提取连接数组。

IGNode *node;
for (NSDictionary *dict in tmpDicts)
{
    node = [[IGNode alloc]init];
    node.lon = [[dict valueForKey:@"longitude"] doubleValue];
    node.lat = [[dict valueForKey:@"latitude"] doubleValue];
    node.links = [[dict valueForKey:@"connections"]mutableCopy];

    // Do something with the node (like add it to an array?)
}
于 2013-04-18T16:44:41.910 回答
0

尝试

for (NSDictionary *nodeDict in plistData) {

    IGNode *node = [self.nodes objectAtIndex:i];

    node.lon   = [nodeDict[@"longitude"] doubleValue];
    node.lat   = [nodeDict[@"latitude"] doubleValue];
    node.links = [nodeDict[@"connections"] mutableCopy];

}
于 2013-04-18T16:39:33.353 回答