我使用核心数据,我想调用一个函数来返回一个对象。这个对象在这个函数中用AFNetworking完成成一个完成块。我想知道是否可以将此对象返回到此完成块中……以供以后使用。此处代码失败,因为完成块中的 newMember 不允许且无法正常工作。
出现此错误:
Incompatible block pointer types sending 'Member *(^)(NSURLRequest...'
+(Member *)returnMemberModelWithId:(NSString *)myid withContext:(NSManagedObjectContext *)managedObjectContext{
__block NSMutableArray *dicoNe =[[NSMutableArray alloc] init];
__block Member *newMember = [NSEntityDescription
insertNewObjectForEntityForName:@"Member"
inManagedObjectContext:managedObjectContext];
NSString *s= @".json";
NSString *str = [NSString stringWithFormat: @"%@%@", myid, s];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://epnet.fr/"]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setParameterEncoding:AFFormURLParameterEncoding];
NSString *path = [NSString stringWithFormat:@"members/%@", str];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:path parameters:nil];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *innerRequest, NSHTTPURLResponse *response, id reponseJSON) {
dicoNe = reponseJSON;
NSNumber *v = [dicoNe valueForKey:@"id"];
newMember.idMember = v; //And others assignments ..
return newMember; // It's not working
} failure:^(NSURLRequest *innerRequest, NSHTTPURLResponse *response, NSError *error, id reponseJSON) {
NSLog(@"Fail %@", reponseJSON);
}];
[operation start];
return newMember;
}
因此,我尝试返回的对象 newMember 最后为 null。所以我的问题是,我怎样才能用这个函数返回由 AFN 完成的对象?非常感谢