我正在尝试根据this示例访问home_timeline请求。

但是,我不断收到以下错误:


'URLRequestWithMethod',其参数类型为'(String,URL:String,parameters:NSArray,error:inout NSError?)'

func getHomeTimeLine(){
    var clientError:NSError?
    let params = []

    let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod(
        "GET",
        URL: "https://api.twitter.com/1.1/statuses/home_timeline.json",
        parameters: params,
        error: &clientError)

    if request != nil {
        Twitter.sharedInstance().APIClient.sendTwitterRequest(request) {
                (response, data, connectionError) -> Void in
                if (connectionError == nil) {
                    var jsonError : NSError?
                    let json : AnyObject? =
                    NSJSONSerialization.JSONObjectWithData(data,
                        options: nil,
                        error: &jsonError)
                }
                else {
                    println("Error: \(connectionError)")
                }
        }
    }
    else {
        println("Error: \(clientError)")
    }

}

提前致谢。

最佳答案

将params定义为字典并使用它。

let params:字典= Dictionary()

func getHomeTimeLine() {
    var clientError:NSError?
    let params: Dictionary = Dictionary<String, String>()

    let request: NSURLRequest! = Twitter.sharedInstance().APIClient.URLRequestWithMethod(
        "GET",
        URL: "https://api.twitter.com/1.1/statuses/home_timeline.json",
        parameters: params,
        error: &clientError)

    if request != nil {
        Twitter.sharedInstance().APIClient.sendTwitterRequest(request!) {
            (response, data, connectionError) -> Void in
            if (connectionError == nil) {
                var jsonError : NSError?
                let json : AnyObject? =
                NSJSONSerialization.JSONObjectWithData(data!,
                    options: nil,
                    error: &jsonError)

                // check for json data
                if (json != nil) {
                    println("response = \(json)")
                } else {
                    println("error loading json data = \(jsonError)")
                }
            }
            else {
                println("Error: \(connectionError)")
            }
        }
    }
    else {
        println("Error: \(clientError)")
    }
}

关于ios - 使用Swift的Twitter API无效的URLRequestWithMethod,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30832991/

10-09 23:04