How return data from an HTTP request in Swift/Objective C

前端 未结 4 1302
無奈伤痛
無奈伤痛 2021-01-15 14:41

I\'m trying to use Coinbase\'s API to get information about my online bitcoin wallet, and I\'m trying to use Swift\'s NSURLSession object to do so. Perhaps I\'m missing some

4条回答
  •  爱一瞬间的悲伤
    2021-01-15 14:55

    In objective-C you can use __block and get the data when the operation finishes:

    __block NSData *myData;
    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:urlString]
          completionHandler:^(NSData *data,
                              NSURLResponse *response,
                              NSError *error) {
            myData = data;
    
    }] resume];
    

提交回复
热议问题