2012-07-10 78 views
-2

可能重複:
displaying a random word from a plist從plist中選擇隨機字

我有它的一些字的plist現在我會盡量顯示plist中的一個隨機單詞,但我的應用程序崩潰我不知道如何解決這個問題。

這是我的代碼:

NSArray *randomAddons = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wordsENG" ofType:@"plist"]]; 

NSLog(@"Array: %@", randomAddons); 

int randomIndex = arc4random() % [randomAddons count]; 

mainTextController.text = [username2 stringByAppendingString:[randomAddons objectAtIndex:randomIndex]]; 

這是崩潰:

Array: (
    (
    DANDAN, 
    DONDON, 
    SAMSAM, 
    SANSAN 
) 
) 
2012-07-10 11:23:28.047 spinningyarn[336:1bb03] -[__NSCFArray length]: unrecognized  selector sent to instance 0xa26cad0 
2012-07-10 11:23:28.047 spinningyarn[336:1bb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0xa26cad0' 
*** First throw call stack: 
(0x1b04022 0x1ee0cd6 0x1b05cbd 0x1a6aed0 0x1a6acb2 0x1468bd9 0x115dc3 0x3de995 0x979ed9 0x981725 0x8ea4ab 0x9837c6 0x8c9885 0x2166330 0x2168509 0x1a3b803 0x1a3ad84 0x1a3ac9b 0x26287d8 0x262888a 0xb3d626 0x29e2 0x2955 0x1) 
terminate called throwing an exception 

我希望有人能幫助我

+0

[從plist中顯示的隨機字]的轉播(http://stackoverflow.com/questions/11405047/displaying-a-random-word-from-a-plist)。請不要這樣做。 – 2012-07-10 17:12:52

回答

3

randomAddons是包含一個數組的數組...

嘗試:

NSArray *randomAddons = [(NSArray*)[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wordsENG" ofType:@"plist"]] objectAtIndex:0]; 

NSLog(@"Array: %@", randomAddons); 

int randomIndex = arc4random() % [randomAddons count]; 

mainTextController.text = [username2 stringByAppendingString:[randomAddons objectAtIndex:randomIndex]]; 
+0

打我吧^^ +1 – borrrden 2012-07-10 09:38:44

+0

這個工程!非常感謝!現在就可以開始使用我的項目!:D – 2012-07-10 09:43:04

+0

然後,如果答案被接受,這將是很好的。謝謝 – javieralog 2012-07-10 13:13:45