Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting a random string from a plist and displaying it in a text field
    primarykey
    data
    text
    <p>Hey I'm very new to iPhone programming and I have been searching for hours both on this site and on google for the solution to my problem. Basically I am trying to make an app which selects a random string out of my plist and displays it into a text field. I have tried countless ways, but this way seems to work the best, however it still crashes when I press a button and gives me the error "EXC_ARITHMETIC" on the arc4random line. As well as this error, the NSLog(@"items: &amp;@", items) displays items: (null) in the log when i press the button. Any help or suggestions are GREATLY appreciated. P.S. items is an NSMutableArray I already set up in the .h file</p> <pre><code>-(IBAction)buttonPress { NSString* path = [[NSBundle mainBundle] pathForResource:@"ItemList" ofType:@"plist"]; items = [[NSMutableArray alloc]initWithContentsOfFile:path]; NSLog(@"items: %@", items); NSInteger randomIndex = arc4random() % [items count]; NSString* randomString = [items objectAtIndex:randomIndex]; textField.text = randomString; } </code></pre> <p>Here's the plist code</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Array&lt;/key&gt; &lt;array&gt; &lt;string&gt;AAA&lt;/string&gt; &lt;string&gt;BBB&lt;/string&gt; &lt;string&gt;CCC&lt;/string&gt; &lt;string&gt;DDD&lt;/string&gt; &lt;string&gt;EEE&lt;/string&gt; &lt;string&gt;FFF&lt;/string&gt; &lt;string&gt;GGG&lt;/string&gt; &lt;string&gt;HHH&lt;/string&gt; &lt;string&gt;III&lt;/string&gt; &lt;string&gt;JJJ&lt;/string&gt; &lt;/array&gt; </code></pre> <p> </p> <p>Thanks guys again for all your help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload