gpt4 book ai didi

swift - 如何在 Swift 中从 NSMutableURLRequest 获取数据?

转载 作者:行者123 更新时间:2023-11-30 10:03:12 25 4
gpt4 key购买 nike

我尝试通过以下代码访问字典 API:

func apiConnection() {
if let url = NSURL(string:"https://wordsapiv1.p.mashape.com/words/soliloquy") {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("aaabbbccc", forHTTPHeaderField: "X-Mashape-Key")
request.addValue("application/json", forHTTPHeaderField: "Accept")
print(request.HTTPBody)

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("\(error)")
return
}

print("\(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("\(responseString)")
}
}
}

我的代码有问题吗?抱歉,我在这个领域还很陌生。我使用的API来自https://market.mashape.com/wordsapi/wordsapi

最佳答案

您配置了任务,但未启动它

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
...
}
task.resume() // Start the task

关于swift - 如何在 Swift 中从 NSMutableURLRequest 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com