我需要重写一个Objective-C块,但我不能
这是有问题的街区:
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLDriveFileList* files, NSError *error) {
}];
在Swift中,我尝试过:
service.executeQuery(query, completionHandler: {(ticket:GTLServiceTicket!,files:GTLDriveFileList!,error:NSError!) -> Void in
})
但是编译器并不同意。
我试过了:
service.executeQuery(query, completionHandler: {(ticket:GTLServiceTicket?,files:GTLDriveFileList?,error:NSError?) -> () in
})
但同样的问题。
当我cmd +点击课程时,我发现:
// Clients should create a standard query with any of the class methods in
// GTLQueryDrive.h. The query can the be sent with GTLService's execute methods,
//
// - (GTLServiceTicket *)executeQuery:(GTLQuery *)query
// completionHandler:(void (^)(GTLServiceTicket *ticket,
// id object, NSError *error))handler;
有谁知道怎么做?
由于
答案 0 :(得分:1)
完成处理程序第二个参数是一个id对象(obj c),因此在swift中它将成为AnyObject
swift< 2.3 -
service.executeQuery(query, completionHandler: {(ticket:GTLServiceTicket!, object: AnyObject!, error: NSError!)->Void in
})
swift 3.0 -
因为objc代码中的id
参数在Swift 3.0中有Any
类型,因此 -
service.executeQuery(query, completionHandler: {(ticket:GTLServiceTicket?, object: Any?, error: Error?)->Void in
})