gpt4 book ai didi

iOS RestKit block

转载 作者:行者123 更新时间:2023-11-28 22:52:46 24 4
gpt4 key购买 nike

我正在尝试使用带 block 的 Restkit 等待响应。

例子:

NSArray *myArray = ["RESULT OF REST-REQUEST"];

// Working with the array here.

我的一个阻止请求:

- (UIImage*)getPhotoWithID:(NSString*)photoID style:(NSString*)style authToken:(NSString*)authToken {
__block UIImage *image;
NSDictionary *parameter = [NSDictionary dictionaryWithKeysAndObjects:@"auth_token", authToken, nil];
RKURL *url = [RKURL URLWithBaseURLString:@"urlBase" resourcePath:@"resourcePath" queryParameters:parameter];
NSLog(@"%@", [url absoluteString]);
[[RKClient sharedClient] get:[url absoluteString] usingBlock:^(RKRequest *request) {
request.onDidLoadResponse = ^(RKResponse *response) {
NSLog(@"Response: %@", [response bodyAsString]);
image = [UIImage imageWithData:[response body]];
};
}];
return image;
}

最佳答案

您不能在此方法中返回任何内容,因为图像的获取将是异步的 - 它必须是 -(void)

那么,你是做什么的?您应该将调用此方法的操作放在响应 block 中。警惕retain cycles在 block 中。

__block MyObject *selfRef = self;

[[RKClient sharedClient] get:[url absoluteString] usingBlock:^(RKRequest *request) {
request.onDidLoadResponse = ^(RKResponse *response) {

NSLog(@"Response: %@", [response bodyAsString]);

image = [UIImage imageWithData:[response body]];

[selfRef doSomethingWithImage:image];

};
}];

关于iOS RestKit block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11518753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com