gpt4 book ai didi

ios - 加载存储在 plist 中的数组

转载 作者:行者123 更新时间:2023-11-29 03:59:16 24 4
gpt4 key购买 nike

我有一个 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中?

最佳答案

你试过这个吗......

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?)
}

关于ios - 加载存储在 plist 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088433/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com