Dou*_*ith 4 cocoa-touch objective-c swift ios8
我正在使用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
}];
}];
Run Code Online (Sandbox Code Playgroud)
这是我在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
})
})
Run Code Online (Sandbox Code Playgroud)
但是我一直在使用Swift版本得到这个错误:
找不到接受提供的参数的'init'的重载
编辑:这是一个显示它的示例项目:http://cl.ly/3K0i2P1r3j1y
注意:此问题与以下问题相同:
animateWithDuration:动画:完成:在Swift中
感谢您添加示例项目,问题如下:
从Swift书中:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html
闭包的优化之一是:
单表达式闭包的隐式返回
所以...编译器认为你的闭包返回一个值,NSURLSessionDataTask因为它是闭包块内的唯一一行,从而改变了参数的类型.
有几种方法可以解决这个问题,没有一种方法是理想的......
这个想法是你添加到闭包中的任何其他行都将修复它,所以这将完全有效:
RKClient.sharedClient().signInWithUsername("username", password: "password", completion: { error -> () in
let a = 1
RKClient.sharedClient().frontPageLinksWithPagination(nil, completion: nil)
})
Run Code Online (Sandbox Code Playgroud)
解决这个问题的一种稍微简洁的方法是:
RKClient.sharedClient().signInWithUsername("username", password: "password", completion: { error in
let client = RKClient.sharedClient()
client.frontPageLinksWithPagination(nil, completion: nil)
})
Run Code Online (Sandbox Code Playgroud)
如果您只是将更多代码放入该闭包中,这也不会成为问题!
| 归档时间: |
|
| 查看次数: |
925 次 |
| 最近记录: |