问题描述:
这些天在做AD,NSDictionar读取出来的顺序不是plist文件中的顺序。
问题分析:
试了各种库和方法,无效,网上搜了一些不算解决办法的方法 。
问题解决:
(1) Keep an array of keys along with the dictionary. Then loop through the array and use the keys to get the values. (You'll have to add your own checks to keep the same key from appearing in the array twice.)
1,简单说来就是,把nsdictionary的所有key组成的nsarray按自己的要求排序,然后根据这个key组成的array来获取对应的value的array。key的array不要扔掉,每次要按顺序获取值的时候都需要它。
(2)You could keep an array of custom objects, one custom object for each pair (give the object .key and .value properites) . That's a very object-oriented way, but you lose the ability to easily find the value for a key (you have to loop through the array.)
2,不直接用nsdictionary,而是用一个nsarray,里面每个object都是一个只有一对key-value的nsdictionary。这个方法的缺点是,找某个value或者key会变得很麻烦,需要遍历。
(3)You could keep two arrays of strings, one for keys and one for values.
to find a value for a key, you just use
keyIndex = [myKeys indexOfObject: keyIWant];
keyValue = [myValues objectAtIndex: keyIndex];
3,不用nsdictionary,而是用两个对应的nsarray。相互调用,分别做key和value。
《NSDictionary读plist文件顺序问题》上有1条评论