Cocos2d iphone 按正确顺序从plist读取项目的范围
在cocos2d for iPhone中,是否有办法将以下内容转换为for循环,以便指定一个值范围?目前,它只是遍历所有值(但顺序不正确) 我希望能够只打印一系列项目,比如10到15项(按照数据在列表中的顺序)。如何才能做到这一点Cocos2d iphone 按正确顺序从plist读取项目的范围,cocos2d-iphone,plist,Cocos2d Iphone,Plist,在cocos2d for iPhone中,是否有办法将以下内容转换为for循环,以便指定一个值范围?目前,它只是遍历所有值(但顺序不正确) 我希望能够只打印一系列项目,比如10到15项(按照数据在列表中的顺序)。如何才能做到这一点 编辑:如果我想使用数组而不是dict(保持顺序),我需要如何更改以下列表 个人主义 名称 汤姆 年龄 30 个人詹姆斯 名称 詹姆斯 年龄 45 迭代NSDictionary元素会以随机顺序显示元素。字典没有分类 如果排序对您很重要,请使用NSArray存储元素并
编辑:如果我想使用数组而不是dict(保持顺序),我需要如何更改以下列表
个人主义
名称
汤姆
年龄
30
个人詹姆斯
名称
詹姆斯
年龄
45
迭代NSDictionary元素会以随机顺序显示元素。字典没有分类
如果排序对您很重要,请使用NSArray存储元素并对其进行排序。或者,您可以读取字典的所有键并将它们放入临时NSArray中,然后对该数组进行排序,在数组中的键上迭代以按排序顺序获得字典的值。详细说明:NSArray*_sortedKeys=[[NSArray arrayWithArray:[列出所有键]]sortedArrayUsingSelector:@selector(compare:)];。然后迭代_sortedKeys,以正确的顺序获取对象。即使你不喜欢这个答案,它也是正确的!
// plist file
NSString * file = @"myproperties";
NSString * dictPath = [[NSBundle mainBundle] pathForResource:file
ofType:@"plist"];
// get dictionary
NSMutableDictionary * dictPlist = [[NSMutableDictionary alloc]
initWithContentsOfFile:dictPath];
NSEnumerator *enumerator = [dictPlist objectEnumerator ];
id value;
while ((value = [enumerator nextObject])) {
CCLog("Test: @%", [value objectForKey:@"Name"]);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>personTom</key>
<dict>
<key>Name</key>
<string>Tom</string>
<key>Age</key>
<integer>30</integer>
</dict>
<key>personJames</key>
<dict>
<key>Name</key>
<string>James</string>
<key>Age</key>
<integer>45</integer>
</dict>
<!-- (...) -->
</dict>
</plist>