我triyng從服務器獲取數組。但它不起作用。我的意思是在函數中,一切都與我的數組沒有關係,但在它之外它是空的。怎麼修?客觀c隱形陣列超出功能參數請求
for(int i=1; i<5; i++){
NSString *category = [NSString stringWithFormat:@"%d",i];
NSString *encrypt = @"encrypt=93mrLIMApU1lNM619WzZje4S9EeI4L2L";
NSString *latitude = @"latitude=32.794044";
NSString *longtitude = @"longitude=34.989571";
NSString *params = [NSString stringWithFormat:@"%@&category=%@&%@&%@&area=CENTER",
encrypt,category,latitude,longtitude];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://admin.t-club.co.il/api/get-buissness"]];
NSData *postBody = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
{
if(!connectionError)
{
_myDict =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
_tmpArray = [_myDict objectForKey:@"result"];
NSLog(@"my array %@",_tmpArray);//here array isn't null
}
}];
[_myArray addObjectsFromArray:_tmpArray];
}
NSLog(@"my array %@",_tmpArray);//here is null
我該如何延遲數組才能滿? –
我已更新我的回答 – tommybananas
我喜歡你原來的答案,但不是編輯。不要用'sendSynchronousRequest'來阻塞主體,特別是在循環中。 OP需要關於如何異步和串行執行這些請求的建議。 – danh