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

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

The canvas has been tainted by cross-origin data in chrome only -