我想解析來自plist文件的數據到NSMutableArray中。從NSMutableArray提取值的問題
以我的plist 根是含有的6號
我已經創建與IBOutlet中的UILabel * lbl4對象鉤住的標籤陣列的字典,我想此標籤以顯示陣列所作的第一元件閱讀plist。問題是程序在分配指令(最後一個)上崩潰。 我的代碼是這樣的:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *plistPath = [docPath stringByAppendingPathComponent:@"settings.plist"];
if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]);
{
plistPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *err = nil;
NSPropertyListFormat format;
NSDictionary *temp = (NSDictionary *) [NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&err];
if(!temp)
{
NSLog(@"Error reading plist: %@, format: %d", err, format);
}
self.dataSet = [NSMutableArray arrayWithArray:[temp objectForKey:@"Dadi"]];
[lbl4 setText:[NSString stringWithFormat:@"%@", [dataSet objectAtIndex:0]]];
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">
<dict>
<key>Dadi</key>
<array/>
<key>D4</key>
<integer>0</integer>
<key>D6</key>
<integer>0</integer>
<key>D8</key>
<integer>0</integer>
<key>D10</key>
<integer>0</integer>
<key>D12</key>
<integer>0</integer>
<key>D20</key>
<integer>0</integer>
</dict>
</plist>
調試輸出表示「2012-08-30 18:29:55.483信仰[6014:707] *終止應用程序由於未捕獲的異常「NSRangeException」,原因是:「* - [__ NSArrayM objectAtIndex:]:索引0超越界限空陣」」
什麼是崩潰錯誤信息? –
而且,你能顯示房產清單嗎? (隨意虛化任何實際的字符串) –
添加plist和調試輸出的問題! –