在Swift中调用Slack Webincoming钩子但得到“中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)”

时间:2023-01-23 21:33:25

I've use Swift to post something to Slack use Webhook as an POST request, but get an error like

我使用Swift发布一些东西给Slack使用Webhook作为POST请求,但是得到一个错误

interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

in the line of var request = .... Can anyone tell me why I get such an error? Thanks!! :D

在var request = ....的行中有谁能告诉我为什么会出现这样的错误?谢谢!! :d

("Webhook URL here" refers to a real proper URL, but when post this question I just replace it with "Webhook URL here".)

(“这里的Webhook URL”指的是一个真实的URL,但在发布此问题时,我只需将其替换为“此处的Webhook URL”。)

import UIKit
import XCPlayground

let str = "payload={'channel': '#test', 'username': 'webhookbot', 'text': 'This is posted to #test and comes from a bot named webhookbot.', 'icon_emoji': ':ghost:'}"
let strData = (str as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData

var request = NSMutableURLRequest(URL: NSURL(string: "Webhook URL here")!, cachePolicy: cachePolicy, timeoutInterval: 2.0)

request.HTTPMethod = "POST"
request.HTTPBody = strData

var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
let results = NSString(data:data!, encoding:NSUTF8StringEncoding)

1 个解决方案

#1


1  

You should also use optional binding to unwrap your data

您还应该使用可选绑定来解包数据

if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {
    let results = NSString(data:data, encoding:NSUTF8StringEncoding)
}

You can also try logging the error in the synchronous request like the code below.

您也可以尝试在同步请求中记录错误,如下面的代码。

So your final code should be something like this

所以你的最终代码应该是这样的

import UIKit
import XCPlayground

let str = "payload={'channel': '#test', 'username': 'webhookbot', 'text': 'This is posted to #test and comes from a bot named webhookbot.', 'icon_emoji': ':ghost:'}"
let strData = (str as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData

if let url = NSURL(string: "Your Webhook Url")
{
    var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 2.0)

    request.HTTPMethod = "POST"
    request.HTTPBody = strData

    var error : NSError? = nil
    if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: &error) {
       let results = NSString(data:data, encoding:NSUTF8StringEncoding)
    }
    else
    {
        println("data invalid")
        println(error)
    }
}
else {
    println("url invalid")
}

#1


1  

You should also use optional binding to unwrap your data

您还应该使用可选绑定来解包数据

if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil) {
    let results = NSString(data:data, encoding:NSUTF8StringEncoding)
}

You can also try logging the error in the synchronous request like the code below.

您也可以尝试在同步请求中记录错误,如下面的代码。

So your final code should be something like this

所以你的最终代码应该是这样的

import UIKit
import XCPlayground

let str = "payload={'channel': '#test', 'username': 'webhookbot', 'text': 'This is posted to #test and comes from a bot named webhookbot.', 'icon_emoji': ':ghost:'}"
let strData = (str as NSString).dataUsingEncoding(NSUTF8StringEncoding)
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData

if let url = NSURL(string: "Your Webhook Url")
{
    var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 2.0)

    request.HTTPMethod = "POST"
    request.HTTPBody = strData

    var error : NSError? = nil
    if let data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: &error) {
       let results = NSString(data:data, encoding:NSUTF8StringEncoding)
    }
    else
    {
        println("data invalid")
        println(error)
    }
}
else {
    println("url invalid")
}