Skip to content

Instantly share code, notes, and snippets.

@keicoder
Created March 9, 2014 05:04
Show Gist options
  • Save keicoder/9443131 to your computer and use it in GitHub Desktop.
Save keicoder/9443131 to your computer and use it in GitHub Desktop.
objective-c : generate random number with array
//generate random number with array
- (void)awakeFromInsert //invoked automatically by the Core Data framework when the receiver is first inserted into a managed object context.
{
[super awakeFromInsert];
NSString *quotesFilePath = [[NSBundle mainBundle]
pathForResource:@"QuotesList"
ofType:@"plist"];
NSArray *quotesArray = [NSArray arrayWithContentsOfFile:quotesFilePath];
NSLog (@"quotesArray: %@", quotesArray);
NSUInteger quoteIndex = arc4random() % [quotesArray count]; //generate random number
NSDictionary *quoteDictionary = quotesArray[quoteIndex];
NSLog (@"quoteDictionary: %@", quoteDictionary);
[self setPrimitiveValue:quoteDictionary[@"personName"]
forKey:@"personName"];
[self setPrimitiveValue:quoteDictionary[@"famousQuote"]
forKey:@"famousQuote"];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment