我在Swift中做错了什么来调用这个Objective-C块/ API调用?
我正在使用
RedditKit将Reddit集成到一个应用程序中,而在Objective-C中,我调用API如下(并且它工作正常):
[[RKClient sharedClient] signInWithUsername:@"username" password:@"password" completion:^(NSError *error) { RKPagination *pagination = [RKPagination paginationWithLimit:100]; [[RKClient sharedClient] linksInSubredditWithName:subredditSelected pagination:pagination completion:^(NSArray *collection,RKPagination *pagination,NSError *error) { // code that executes on completion }]; }]; 这是我在Swift中调用它的方式: RKClient.sharedClient().signInWithUsername("username",password: "password",completion: { (error: NSError!) in RKClient.sharedClient().frontPageLinksWithPagination(RKPagination(limit: 50),completion: { (collection: RKLink[]!,pagination: RKPagination!,error: NSError!) in // code that executes on completion }) }) 但是我一直在使用Swift版本得到这个错误:
编辑:这是一个显示它的示例项目:http://cl.ly/3K0i2P1r3j1y 解决方法
注意:此问题与以下问题相同:
animateWithDuration:animations:completion: in Swift Properly referencing self in dispatch_async 感谢您添加示例项目,问题如下: 来自斯威夫特书:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html 闭包的优化之一是:
所以……编译器认为你的闭包返回NSURLSessionDataTask的值,因为它是闭包块内的唯一一行,从而改变了参数的类型. 有几种方法可以解决这个问题,没有一种方法是理想的…… 这个想法是你添加到闭包中的任何其他行都将修复它,所以这将完全起作用: RKClient.sharedClient().signInWithUsername("username",completion: { error -> () in let a = 1 RKClient.sharedClient().frontPageLinksWithPagination(nil,completion: nil) }) 解决这个问题的一种稍微简洁的方法是: RKClient.sharedClient().signInWithUsername("username",completion: { error in let client = RKClient.sharedClient() client.frontPageLinksWithPagination(nil,completion: nil) }) 如果您只是将更多代码放入该闭包中,这将不会成为问题! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |