Ios 加载随机APi内容
如何更改此函数以从API随机加载内容?我想传入一个随机整数并更改url的端点Ios 加载随机APi内容,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,如何更改此函数以从API随机加载内容?我想传入一个随机整数并更改url的端点 NSString* WebServiceURL =@"http://movie-quotes.herokuapp.com/api/v1/quotes"; 对此 http://movie-quotes.herokuapp.com/api/v1/quotes/2 其中,末尾的数字是报价编号 这是我的相关代码: -(void)LoadQuotesRandom { randomQuotes = nil; dispat
NSString* WebServiceURL =@"http://movie-quotes.herokuapp.com/api/v1/quotes";
对此
http://movie-quotes.herokuapp.com/api/v1/quotes/2
其中,末尾的数字是报价编号
这是我的相关代码:
-(void)LoadQuotesRandom
{
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 Customer 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];
}
});
});
}
更简单地说,我想您需要一个带有随机整数后缀的url字符串
NSInteger randomInt = arc4random_uniform(SOME_MAX);
NSString *baseUrl = @"http://movie-quotes.herokuapp.com/api/v1/quotes";
NSString *webServiceUrl = [NSString stringWithFormat:@"%@/%ld", baseUrl, randomInt];
您需要为可以请求的最大报价索引定义一些最大值
我建议您将此代码与发出请求的代码完全分开。在给定描述url的字符串的情况下,获得一个执行任何请求的工件,以及生成这些url字符串的单独工件