Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用TwitterKit为Swift中的直接消息设置urlRequest参数_Swift_Twitter_Twitterkit - Fatal编程技术网

使用TwitterKit为Swift中的直接消息设置urlRequest参数

使用TwitterKit为Swift中的直接消息设置urlRequest参数,swift,twitter,twitterkit,Swift,Twitter,Twitterkit,有了Swift,我可以使用以下代码毫无问题地发布推文: func postTweet(tweet : String) { if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) { var clientError : NSError? let session = TWTRTwitter.sharedInstance().sessionStore.session()

有了Swift,我可以使用以下代码毫无问题地发布推文:

func postTweet(tweet : String) {
    if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
        var clientError : NSError?
        let session = TWTRTwitter.sharedInstance().sessionStore.session()

        if let userid = session?.userID {

            let client = TWTRAPIClient(userID: userid)
            let urlEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
            let params = ["status": "This works fine."]

            let request = client.urlRequest(withMethod: "POST",
                 urlString: urlEndpoint, 
                 parameters: params, error: &clientError)

            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
                if connectionError != nil {
                    print("Error: \(String(describing: connectionError?.localizedDescription))")
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: [])
                    print("json: \(json)")

                } catch let jsonError as NSError {
                    print("json error: \(jsonError.localizedDescription)")
                }
            }
        }
    }
}
-

然而,当我为了发送直接消息而更改URL端点和参数时,我最终会在应用程序中崩溃。下面的错误消息&我不知道为什么。谢谢你的帮助

func postDM(tweet : String) {
    if (TWTRTwitter.sharedInstance().sessionStore.hasLoggedInUsers()) {
        var clientError : NSError?
        let session = TWTRTwitter.sharedInstance().sessionStore.session()
        let statusesShowEndpoint = "https://api.twitter.com/1.1/direct_messages/events/new.json"

        // '{"event": {"type": "message_create", "message_create": {"target": {"recipient_id": "RECIPIENT_USER_ID"}, "message_data": {"text": "Hello World!"}}}}'

        let param = [
            "event": [
                "type":"message_create",
                "message_create": [
                    "target": ["recipient_id": "6WCbot"],
                    "message_data": ["text": "\(tweet)"]
                ]
            ]
        ]

        if let userid = session?.userID {

            let client = TWTRAPIClient(userID: userid)
            //let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/update.json"
            //let param = ["status": "Terceiro teste?"]
            //print (param)

            // CRASH HERE
            var request = client.urlRequest(withMethod: "POST", urlString: statusesShowEndpoint, parameters: param, error: &clientError) 

            request.setValue("application/json", forHTTPHeaderField: "Content-Type")
            request.httpBody = try? JSONSerialization.data(withJSONObject: param, options: [])

            client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
                if connectionError != nil {
                    print("Error: \(String(describing: connectionError?.localizedDescription))")
                }

                do {
                    if data != nil {
                        let json = try JSONSerialization.jsonObject(with: data!, options: [])
                        print("json: \(json)")
                    }

                } catch let jsonError as NSError {
                    print("json error: \(jsonError.localizedDescription)")
                }
            }
        }
    }
}
我收到的“无法识别的选择器”错误消息是


2018-09-14 14:08:55.697266+0200公司地址[35050:15292775] -[\u TtGCs26\u SwiftDeferredensDictionaryssp\uuuuuu\uStringByAddingPercentEncodingwithAllowedCharacters:]:无法识别 选择器发送到实例0x6080023D200 2018-09-14 14:08:55.706728+0200 6WCCompanion[35050:15292775]***终止应用程序 由于未捕获的异常“NSInvalidArgumentException”,原因: '-[\u TtGCs26\u迅捷延迟词典sp__ stringByAddingPercentEncodingWithAllowedCharacters::无法识别 选择器已发送到实例0x6080023D200'


请使用此格式

{ “事件”:{ “类型”:“消息创建”, “创建消息”:{ “目标”:{ “收件人id”:“844385345234” }, “消息数据”:{ “文本”:“嗨,我叫乔恩,有什么可以帮忙的?”, }, “自定义配置文件\u id”:“100001” } } }