0

I made a p list with some words in it now I try to display a random word out of that plist

this is my code

NSArray *randomAddons = [NSArray arrayWithContentsOfFile: @"wordsENG.plist"];
int randomIndex = arc4random() % [randomAddons count];
mainTextController.text = [username2 stringByAppendingString:[randomAddons objectAtIndex:randomIndex]];

This crashes and when I change % [randomAddons count]; to % 3; it crashes to but I don't know how to correctly code this Can someone help me out?

thanks

EDIT:

There was a problem with the plist file I edited that according to the link jackjr300 provided, look at the comment below. Still I am left with the crashes that I got from beginning. the crash says:

Array: (
        (
        SAMSAM,
        SELSEL,
        DONDON
    )
)
2012-07-10 03:41:11.048 spinningyarn[1590:1bb03] -[__NSCFArray length]: unrecognized selector sent to instance 0xcfcfdd0
2012-07-10 03:41:11.048 spinningyarn[1590:1bb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance 0xcfcfdd0'
*** 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(lldb) 

SAMSAM, SELSEL, DONDON being the 3 words in my P list file

Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
Don Kooijman
  • 593
  • 1
  • 4
  • 13
  • Where is the PLIST file located? Did you drag it into Xcode or is it located in the app's documents directory? `wordsENG.plist` by itself will not open the correct file. – Jack Humphries Jul 10 '12 at 00:32
  • Insert `NSLog(@"Array: %@", randomAddons);` right above the `arc4random` line and add the results to your question. If the array returns (null), then you are not accessing the file correctly. – Jack Humphries Jul 10 '12 at 00:34
  • it results: Array: (null). does this mean that my plist file is not correctly? – Don Kooijman Jul 10 '12 at 00:38
  • Yes, if it is (null) then you are not accessing the PLIST file correctly. Where is the PLIST located? Did you drag it into your Xcode project? – Jack Humphries Jul 10 '12 at 00:39
  • I created it within the Xcode project.. right clicked the app, add file etc. New to plist making^^ – Don Kooijman Jul 10 '12 at 00:42
  • It didn't make any difference, When I change % [randomAddons count]; to % 3; the crash says: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFString stringByAppendingString:]: nil argument' – Don Kooijman Jul 10 '12 at 01:06
  • Could you give me a link to your PLIST file so that I can see its structure? – Jack Humphries Jul 10 '12 at 01:06
  • 1
    read this --> http://stackoverflow.com/questions/6210497/arraywithcontentsoffile-cant-read-plist-file – jackjr300 Jul 10 '12 at 01:14
  • Wooh Finally got somewhere I changed my plist file according to the link jackjr300 send me now the NSLOG results all the words in my plist Still crashes on the lines behind that though. – Don Kooijman Jul 10 '12 at 01:37
  • looks like you have a NSArray in a NSArray. `stringByAppendingString:` calls `length` on its parameter. Most likely `[randomAddons objectAtIndex:randomIndex]` is not a NSString which implements the length method. Add `NSLog(@"%@", NSStringFromClass([[randomAddons objectAtIndex:randomIndex] class]));` to figure out if the object is a NSString – Matthias Bauch Jul 10 '12 at 05:27

3 Answers3

1

Problem is plist file is not an array, its a dictionary :)
code should be (Assumption is, there is array with words stored for key (keyForArray):
(correct implementation depends on entires in Dictionary - how rows are added to plist file)

NSDictionary *randomEntriesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wordsENG" ofType:@"plist"]];

NSUInteger dictionaryCount = [randomEntriesDictionary count];
if(nil != dictionaryCount)
{
   NSArray *randomWords = [randomEntriesDictionary objectForKey:keyForArray];
   NSUInteger randomIndex = arc4Random % dictionaryCount;
   NSString *randomWord = [randomWords objectAtIndex:randomIndex];
}
else
{
  // check if file exists  and not empty?
  // is it in plist format ?
  ... do the needed.
}
Tatvamasi
  • 2,537
  • 19
  • 14
0

It appears that your file is not being properly included in your app's main bundle. In the project navigator, click on the project and then select the target for your application. Select the "Build Phases" section, and then look for a "Copy Bundle Resources" build phase. If you don't have one, you can create one using the "Add Build Phase" button.

Once you have a Copy Bundle Resource build phase, make sure your plist file is listed as a bundle resource in that section. If it is not, you can either press the + button to add it, or drag the file from the main project navigator file list into the list of bundle resources.

Once you have done this, you can rebuild and the file should be included in your bundle. If the file is in your bundle, you can then use the [[NSBundle mainBundle] pathForResource:@"wordsENG" ofType@"plist"] as was suggested in another answer

Tim Dean
  • 8,253
  • 2
  • 32
  • 59
-1

as shown your console array in array. So try this hope its helpful for you...

NSArray *randomAddons = [NSArray arrayWithContentsOfFile: @"wordsENG.plist"];

NSArray *randomArray = [[NSArray alloc] initWithObjects:[ randomAddons objectAtIndex:0], nil];

int randomIndex = arc4random() % [randomArray count];
mainTextController.text = [username2 stringByAppendingString:[randomArray objectAtIndex:randomIndex]];
Ayaz
  • 1,398
  • 15
  • 29