我在Swift中做错了什么调用这个Objective-C块/ API调用? [英] What am I doing wrong in Swift for calling this Objective-C block/API call?

查看:110
本文介绍了我在Swift中做错了什么调用这个Objective-C块/ API调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 RedditKit 将Reddit集成到一个应用程序中,而在Objective-C中,我将API如下(并且工作正常):

  [[RKClient sharedClient] signInWithUsername:@usernamepassword:@password completion:^(NSError * error){
RKPagination * pagination = [RKPagination paginationWithLimit:100];
[[RKClient sharedClient] linksInSubredditWithName:subredditSelected分页:分页完成:^(NSArray * collection,RKPagination * pagination,NSError * error){
//完成时执行的代码
}
}];

以下是我在Swift中调用它的方法:



pre> RKClient.sharedClient()。signInWithUsername(username,password:password,completion:{(error:NSError!)in
RKClient.sharedClient .frontPageLinksWithPagination(RKPagination(limit:50),completion:{(collection:RKLink [] !, pagination:RKPagination !, error:NSError!)in
//代码执行完成
})
})

但是我一直收到这个错误的Swift版本:


无法找到接受所提供参数的'init'重载


EDIT:以下是一个示例项目: http://cl.ly/3K0i2P1r3j1y

解决方案

注意:此问题与以下问题相同:



animateWithDuration:animations:completion:in Swift



在dispatch_async中正确引用self






感谢您添加示例项目,问题如下:



从Swift书: https:/ /developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html



Closures的优化之一是:


单一表达式闭包的隐式回报


所以...编译器认为你的闭包是返回一个值 NSURLSessionDataTask ,因为它是闭包块内的唯一一行,从而改变了参数的类型。



有几种方法可以解决这个问题,没有一个是理想的...



你添加到闭包中的任何其他行将修复它,所以这将工作:

  RKClient.sharedClient()。signInWithUsername username,password:password,completion:{error  - > ()in 
let a = 1
RKClient.sharedClient()。frontPageLinksWithPagination(nil,completion:nil)
})

一个更简洁的解决方法是:

  RKClient。 sharedClient()。signInWithUsername(username,password:password,completion:{in 
let client = RKClient.sharedClient()
client.frontPageLinksWithPagination(nil,completion:nil)
})

这不会是一个问题,如果你只是有更多的代码,以及!


I'm using RedditKit to integrate Reddit into an app, and in Objective-C I called the API as follows (and it worked fine):

    [[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
        }];
    }];

Here's how I'm calling it in 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
    })
})

But I keep getting this error with the Swift version:

Could not find an overload for 'init' that accepts the supplied arguments

EDIT: Here's an example project showing it: http://cl.ly/3K0i2P1r3j1y

解决方案

Note: This issue is the same as in these questions:

animateWithDuration:animations:completion: in Swift

Properly referencing self in dispatch_async


Thanks for adding the example project, the issue is as follows:

From the Swift book: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html

One of the optimizations of Closures is:

Implicit returns from single-expression closures

So... the compiler thinks your closure is returning a value of NSURLSessionDataTask because it is the only line inside the closure block, thus changing the type of the argument.

There are a few ways to solve this, none that are ideal...

The idea is that any other line you add into the closure will fix it, so this will literally work:

      RKClient.sharedClient().signInWithUsername("username", password: "password", completion: { error -> () in
            let a = 1
            RKClient.sharedClient().frontPageLinksWithPagination(nil, completion: nil)
        })

A slightly cleaner way to solve this would be:

RKClient.sharedClient().signInWithUsername("username", password: "password", completion: { error in
    let client = RKClient.sharedClient()
    client.frontPageLinksWithPagination(nil, completion: nil)
})

This wouldn't be an issue if you simply had more code to put into that closure as well!

这篇关于我在Swift中做错了什么调用这个Objective-C块/ API调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆