无法使用plist填充数组
我想使用Plist填充我的数组。这是我的plist文件Menu.plist。无法使用plist填充数组
-Item 0,类型:词典,值(1项) - 标题,字符串,联系人
-Item 1,类型:词典,值(1项) - 标题,串,编辑
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}
我试图填充细胞接触和编辑,但我menuArray是没有得到加载我的表视图,我的NSLog检查和* plist中越来越路径,但在self.menuArray其仍显示0对象,而应该是2.我以前做过这个,它工作得很好,我不知道有什么今天出错了。有什么建议么???
根据你的问题的plist内容如下
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
如果该字典的一些对象数组,你可以通过把它
XML格式
<?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">
<array>
<dict>
<key>Title</key>
<string>Contact</string>
</dict>
<dict>
<key>Title</key>
<string>Edit</string>
</dict>
</array>
</plist>
如果是这样的话请尝试以下
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
NSArray *menuDataArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSLog(@"Plist Content Array = %@", menuDataArray);
输出
Plist Content Array = (
{
Title = Contact;
},
{
Title = Edit;
}
)
注:
- 请检查您的plist结构。
- Array alloc-init。
试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
self.menuArray=[[NSMutableArray alloc]init];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];
self.menuArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
}
你必须ALLOC的NSMutableArray。
尝试分配alloc NSMutableArray,但没有工作,menuArray.count仍然返回0个对象。我检查了一个旧程序,并且相同的代码在那里工作正常,没有使用ARC,这个项目使用ARC,它会有什么区别,如果有的话,我应该做些什么改变? – Gamerlegend
你能粘贴你的plist文件的内容吗? 如果你想创建的plist阵列,那么就应该是这样的:
<?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">
<array>
<string>Brand A</string>
<string>Brand B</string>
<string>Brand C</string>
<string>Brand D</string>
</array>
</plist>
在你的情况,你可能尝试从基于字典的plist生成的NSArray。而且你应该使用:
NSArray *array = [dict objectForKey:@"KeyForSomeArray"];
如果你看看你的plist文件,它应该是的NSDictionary不是NSArray的 – Injectios
显示实际的plist文本,并检查它添加到目标(所以复制到应用程序)。 – Wain
你可以将内容粘贴到plist中吗? – Tendulkar