Hi I’ve been looking all over and can’t seem to find an answer to my issue. I have very little experience with programming, especially Objective-C.
In my app there is a plist with a certain number of strings. When the user taps a button, the program counts the number of strings in the plist, creates a random number within that range, and is supposed to read the line of the plist with that specific number.
Say if there were 20 strings in the plist, and the app generated the number 5, then the app is supposed to read the 5th string in the plist.
Is there a way to do such a thing or is there another, more efficient way to accomplish this? ANY help is GREATLY appreciated. Thanks.
Edit:
In my .h file I have
NSMutableArray* myArray;
int plistCount;
int randomNumber;
}
@property (nonatomic, retain) NSMutableArray* myArray;
And in my .m file I have
NSString *path = [[NSBundle mainBundle] pathForResource:@"myplist" ofType:@"plist"];
NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.myArray = tmpArray;
plistCount=[self.resorts count];
randomNumber=arc4random() % plistCount;
I dont know where to go from here.
The above code is selecting a random element from
self.myArray.arc4random_uniformgenerates a number between 0 and the argument (excluded) so usingself.myArray.countas a bound you’ll get a valid random index for the array.NOTE
You have to explicitly check that the array is never empty in order for the above code to work, since
arc4random_uniform(0)returns0, therefore in case of an empty array it will generate aNSRangeException.