Ios 保存到后如何循环nsobject
我正在使用Ios 保存到后如何循环nsobject,ios,objective-c,Ios,Objective C,我正在使用NSMutableArray以列表形式存储来自web服务的项。如何从数组中读取数据以显示随机引用 当我试图避免两次访问Web服务时,还是我把它复杂化了 NSInteger randomInt = arc4random_uniform(3); NSString *baseUrl = @"http://movie-quotes.herokuapp.com/api/v1/quotes"; NSString *webServiceUrl= [NSString stringWithFormat:
NSMutableArray
以列表形式存储来自web服务的项。如何从数组中读取数据以显示随机引用
当我试图避免两次访问Web服务时,还是我把它复杂化了
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 the JSON string from our web serivce (in a background thread)
NSDictionary * dictionary = [JSONHelper loadJSONDataFromURL:webServiceUrl];
dispatch_async(dispatch_get_main_queue(), ^{
randomQuotes = [NSMutableArray array];
// Iterate through the array of quotes records in the dictionary
for (NSDictionary * oneQuote in dictionary)
{
Quotes* newQuotes =[[Quotes alloc] init];
// Add our new Customer record to our NSMutableArray
newQuotes.Quote = [oneQuote objectForKey:@"content"];
newQuotes.FilmName = [oneQuote objectForKey:@"film"];
[randomQuotes addObject:newQuotes];
}
});
});
您可以从这样的数组中获取随机对象
NSUInteger randomIndex = arc4random_uniform(theArray.count);
[theArray objectAtIndex: randomIndex];
只要保留引号数组,您就不需要再次点击web服务。您是要让web服务返回随机引号,还是要从web服务返回的选择中显示随机引号?