2010-02-28 53 views
1

我目前正試圖從plist中獲取數據。未能從plist讀取數組數據 - (null)彈出

基本上,它看起來像這樣:

plist called 'woerter' 
    -> Root key of type Dictionary 
    -> woerter key of type Array 
     -> various Items of type String with string Values 

當我現在嘗試讀取從中隨機字符串,我只得到一個(空)的表達

NSString * path = [[NSBundle mainBundle] bundlePath]; 
NSString * finalPath = [path stringByAppendingPathComponent:@"woerter.plist"]; 
NSDictionary * plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain]; 
NSArray * array = [plistData valueForKey:@"woerter"]; 
NSString * string = [array objectAtIndex:arc4random() %110]; 
NSLog(@"stringtest %@", string); 

但我得到的是

2010-02-28 23:01:58.911 TypeFast [5606:a0f] stringtest(null)

由於objectAtIndex:2返回相同,所以它不是arcrandom的問題。

問題在哪裏?

謝謝(:

+0

您需要逐步通過這個代碼,並找出它不工作。你的任何方法都可能會失敗,返回null,然後級聯,直到最後賦值爲NULL。這就是gdb的作用。 – 2010-02-28 22:06:51

回答

2

你忘了的Alloc的NSDictionary中和的NSArray,所以數組和字典不能保存的值

+0

Eh?提問者是從文件創建NSDictionary,所以沒有必要分配一個,並從字典中獲取數組。有幾個原因可能會失敗,但不發送'alloc'消息不是其中之一。 – 2010-08-31 12:56:46