我正在制作我的第一个完成块,由于某种原因,它无法识别返回类型,因此我无法将值传递回方法调用。
继承我的标题声明
+(void)downloadVenues:(void (^)(NSArray *myVenues, NSError *error))block;
继承我的实施
+(void)downloadVenues:(void (^)(NSArray *myVenues, NSError *error))block{
//download scenes
PFQuery *query = [PFQuery queryWithClassName:@"Venues"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
// We found messages!
myVenues = objects;
NSLog(@"Retrieved %lu messages", (unsigned long)[myVenues count]);
}
}];
并且他在另一个文件中调用它
//download venues
[Venue downloadVenues:^(NSArray *myVenues, NSError *error){
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
NSLog(@"myObjects are: %@", myVenues);
_venues = myVenues;
[self loadAnnotations];
}
}];
由于某种原因,它没有在实现中识别myVenues。为什么以及如何解决这个问题?感谢
答案 0 :(得分:3)
您的实现永远不会调用完成块。 这是你应该做的:
+(void)downloadVenues:(void (^)(NSArray *myVenues, NSError *error))block{
//download scenes
PFQuery *query = [PFQuery queryWithClassName:@"Venues"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
block(nil, error);
}
else {
// We found messages!
block(objects, nil);
NSLog(@"Retrieved %lu messages", (unsigned long)[myVenues count]);
}
}];