简体   繁体   English

来自iPhone plist的数组

[英]Array from iPhone plist

I am trying to get an array from a plist as suggested on an answer here but it's not working. 我正在尝试从这里的答案中建议的plist获取数组,但是它不起作用。 Can someone tell me what I'm missing? 有人可以告诉我我所缺少的吗?

Here is what the plist looks like....another weird thing is that I can't get this to open in a plain text file?? 这是plist的样子。...另一个奇怪的事情是我无法在纯文本文件中打开它? when I do it looks like garbage.....but it looks fine in the property list editor: 当我这样做时,它看起来像垃圾.....但是在属性列表编辑器中看起来不错:

<?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>count</key>
<array>
     <integer>5</integer>
</array>
<key>username</key>
<array>
     <string>johnsmith</string>
</array>
</dict>
</plist>

Here is the code i'm using..... dictionary is a NSMUtableDictionary and accountsArray is an NSMustableArray that are in the .h file. 这是我正在使用的代码..... dictionary是一个.MU文件中的NSMUtableDictionary,accountsArray是一个NSMustableArray。

     NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];

 dictionary = tempDictionary;
 [tempDictionary release];


 NSMutableArray *nameArray = [[NSMutableArray alloc] init];
 nameArray = [dictionary objectForKey:@"username"];

 accountsArray = nameArray;

 [nameArray release];

Am I missing something? 我想念什么吗? Screwing something up? 搞砸了吗?

Thanks... 谢谢...

Well, there are a number of issues afoot; 好吧,还有许多问题在解决。 first of all the plist is probably a binary property list, and not actually an XML property list. 首先,plist可能是二进制属性列表,而不是XML属性列表。 This doesn't matter much on the code side of things, but should serve to explain the issues with opening as plain text. 在代码方面,这无关紧要,但是应该以纯文本格式打开来解释问题。

Second, by releasing tempDictionary , you are also releasing dictionary , as they both point to the same object. 其次,通过释放tempDictionary ,还可以释放dictionary ,因为它们都指向同一个对象。 This should serve to explain why your code doesn't work. 这应该可以解释为什么您的代码不起作用。 Try autorelease , or simply dictionaryWithContentsOfFile: and nix the release phrase alltogether. 尝试使用autorelease ,或者简单地使用dictionaryWithContentsOfFile: :,然后将所有发布短语一起删除。 ( dictionaryWithContentsOfFile: will autorelease its return value.) dictionaryWithContentsOfFile:将自动释放其返回值。)

You also repeat the release mistake on the second phrase; 您还重复第二个短语的发布错误; the assignment operator ( = ) doesn't copy in Objective-C; 赋值运算符( = )不复制到Objective-C中; it assigns . 分配

Edit: The latter is not true when using property notation; 编辑:使用属性符号时后者不正确; assuming that properties are defined; 假设已定义属性; self.accountsArray = nameArray would (probably) copy the array into the object. self.accountsArray = nameArray可能会将数组复制到对象中。

Reedit: Correct code: 重新编辑:正确的代码:

dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];
accountsArray = [dictionary objectForKey:@"username"];

I finally got it working like this: 我终于让它像这样工作:

    dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:accountsFilePath];      


 if (accountsArray == nil){

     accountsArray = [[NSMutableArray alloc]init];
 }

 if (countArray == nil){

     countArray = [[NSMutableArray alloc]init];
 }



 countArray = [dictionary objectForKey:@"count"];
 accountsArray = [dictionary objectForKey:@"username"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM