我一直在关注 Appcoda 的教程:http: //www.appcoda.com/background-transfer-service-ios7/ 但用 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)()。
对此的帮助将不胜感激。