ios - 完成 block 完成后返回一个数组

标签 ios iphone objective-c objective-c-blocks

我有以下功能

- (NSArray*) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    _currentStart = startDate;
    _currentEnd = lastDate;
    
    if(appDelegate.internetActive){
        Webservice *web = [[Webservice alloc]init];
        [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) {
            if(finished){
                [self generateRandomDataForStartDate:startDate endDate:lastDate];
                 // NOW return the self.dataArray
            }
        }];
    }
    return self.dataArray; 
}

我不知道如何在 completionblock 完成时返回 self.dataArray。因为我的 self.dataArray 是在 generateRandomDataForStartDate:startDate 方法中填充的。所以目前函数总是返回一个空数组。

最佳答案

您应该在参数中传递完成处理程序 block 。将返回类型设置为 void

调用者对象将编写以下代码:

[calenderView calendarMonthView:monthView marksFromDate:startDate toDate:lastDate completionHandler:^(NSarray *dataArray){
//Process data array over here
}];

- (void) calendarMonthView:(TKCalendarMonthView*)monthView marksFromDate:(NSDate*)startDate toDate:(NSDate*)lastDate completionHandler:(void (^)(NSArray*))completionBlock{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    _currentStart = startDate;
    _currentEnd = lastDate;

    if(appDelegate.internetActive){
        Webservice *web = [[Webservice alloc]init];
        [web fetchAppointmentsOnCompletionFor:startDate andEnd:lastDate OnCompletion:^(BOOL finished) {
            if(finished){
                [self generateRandomDataForStartDate:startDate endDate:lastDate];
                 completionBlock(self.dataArray);
            }
        }];
    }
    completionBlock(self.dataArray);
}

在调用者代码句柄完成 block 中,响应数组作为 argumnet 接收。

关于ios - 完成 block 完成后返回一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947755/

相关文章:

objective-c - 优化数组搜索,通过比较 Objective C 中的 2 个字符串进行搜索

ios - 选择视频时 iOS 10.3 中的 UIImagePickerController 崩溃

ios - 如何在 swift 中使用 switch 显示 15 行?

iphone - SQLite count(*) 如何得到结果?

iphone - 为什么 CSS 不启动 max-device-width : 480px?

ios - 如何使用单个 Storyboard为不同的屏幕尺寸(例如 : iPhone 3. 5",4",4.7"and 5.5")设置不同的约束?

ios - Swift 3 - 排序数组/字典 : '(AnyObject)' is not a subtype of 'NSString'

ios - 在 iOS 中获取设备位置(仅限国家/地区)

iphone - 使用多个图像缩放 UIScrollView

ios - 如何将所选 Collection View Cell 的图像推送到另一个 View Controller?