Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Swift-Copy completionHandler_Objective C_Swift_Background_Appdelegate_Completionhandler - Fatal编程技术网

Objective c Swift-Copy completionHandler

Objective c Swift-Copy completionHandler,objective-c,swift,background,appdelegate,completionhandler,Objective C,Swift,Background,Appdelegate,Completionhandler,我一直在遵循Appcoda的教程: 但是用swift写。我遇到了这一行代码,我无法在swift中工作 -(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{ AppDelegate *appDelegate = [UIApplication sharedApplication].delegate; // Check if all download tasks have be

我一直在遵循Appcoda的教程: 但是用swift写。我遇到了这一行代码,我无法在swift中工作

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;

    // Check if all download tasks have been finished.
    [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
        if ([downloadTasks count] == 0) {
            if (appDelegate.backgroundTransferCompletionHandler != nil) {
                // Copy locally the completion handler.
                void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler;

                // Make nil the backgroundTransferCompletionHandler.
                appDelegate.backgroundTransferCompletionHandler = nil;

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // Call the completion handler to tell the system that there are no other background transfers.
                    completionHandler();

                    // Show a local notification when all downloads are over.
                    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                    localNotification.alertBody = @"All files have been downloaded!";
                    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
                }];
            }
        }
    }];
}
我不能正确理解的部分是:

void(^completionHandler)() = appDelegate.backgroundTransferCompletionHandler
我有appDelegate.backgroundTransferCompletionHandler变量,但我不知道如何将其分配给void(^completionHandler)()。 swift无法识别void(^completionHandler)()


这方面的帮助将不胜感激。

您可能应该处理应用程序委托,因为这就是
completionHandler
闭包的定义所在。您可能会将
backgroundTransferCompletionHandler
属性定义为一个闭包,该闭包是可选的,可能类似于:

var backgroundTransferCompletionHandler: (() -> ())?

func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
    backgroundTransferCompletionHandler = completionHandler

    // do whatever else you want (e.g. reinstantiate background session, etc.)
}
然后,您在问题中引用的代码的Swift格式副本将获取该
completionHandler
的本地副本,如下所示:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate

let completionHandler = appDelegate.backgroundTransferCompletionHandler
然后称之为:

completionHandler?()

是否应将此void(^completionHandler)()替换为void(^completionHandler)(void),谢谢Rob。完美地工作