ios - How do i loop an nsobject once saved to -
i using nsmutablearray
store items in list form web service. how read data out of array display random quote.
as trying avoid hitting webservice twice or on complicating it?
nsinteger randomint = arc4random_uniform(3); nsstring *baseurl = @"http://movie-quotes.herokuapp.com/api/v1/quotes"; nsstring *webserviceurl= [nsstring stringwithformat:@"%@/%ld", baseurl,(long)randomint]; randomquotes = nil; dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0); dispatch_async(queue, ^{ // load json string our web serivce (in background thread) nsdictionary * dictionary = [jsonhelper loadjsondatafromurl:webserviceurl]; dispatch_async(dispatch_get_main_queue(), ^{ randomquotes = [nsmutablearray array]; // iterate through array of quotes records in dictionary (nsdictionary * onequote in dictionary) { quotes* newquotes =[[quotes alloc] init]; // add our new customer record our nsmutablearray newquotes.quote = [onequote objectforkey:@"content"]; newquotes.filmname = [onequote objectforkey:@"film"]; [randomquotes addobject:newquotes]; } }); });
you can random object out of array this.
nsuinteger randomindex = arc4random_uniform(thearray.count); [thearray objectatindex: randomindex];
as long keep array of quotes around, won't need hit web service again.
Comments
Post a Comment