Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 保存到后如何循环nsobject_Ios_Objective C - Fatal编程技术网

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服务返回的选择中显示随机引号?