gpt4 book ai didi

ios - 在 iOS 上使用 swift 消费 REST API 时出错

转载 作者:行者123 更新时间:2023-11-30 13:39:03 26 4
gpt4 key购买 nike

我是 iOS 开发新手,我正在学习本教程:https://grokswift.com/simple-rest-with-swift/我不知道为什么下面的代码总是返回“占位符”并且我在控制台的输出上看不到错误:

import Foundation

class Resolver{
func doSomething() -> String{
var result = "the placeholder"
print("inside doSomething")
let postEndpoint: String = "http://jsonplaceholder.typicode.com/posts/1"
guard let url = NSURL(string: postEndpoint) else {
print("Error: cannot create URL")
return "error here"
}

let urlRequest = NSURLRequest(URL: url)

let config = NSURLSessionConfiguration.defaultSessionConfiguration()

let session = NSURLSession(configuration: config)
print("another thing")

let task = session.dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) in
guard let responseData = data else {
print("Error: did not receive data")
return
//return "error here"
}
print("hereeeeeee ############")
guard error == nil else {
print("error calling GET on /posts/1")
print(error)
return
//return "error here"
}
// parse the result as JSON, since that's what the API provides
let post: NSDictionary
do {
post = try NSJSONSerialization.JSONObjectWithData(responseData,
options: []) as! NSDictionary
} catch {
print("error trying to convert data to JSON")
return
//return "error here"
}
// now we have the post, let's just print it to prove we can access it
print("The post is: " + post.description)
result = post.description

// the post object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
if let postTitle = post["title"] as? String {
print("The title is: " + postTitle)
}

})
task.resume()

return result
}

}

这是我的 info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string></string>
<key>CFBundleExecutable</key>
<string></string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleName</key>
<string></string>
<key>CFBundleDisplayName</key>
<string></string>
<key>CFBundleVersion</key>
<string></string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>httpbin.org</key>
<dict>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>jsonplaceholder.typicode.com </key>
<dict>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>CFBundleShortVersionString</key>
<string></string>
<key>CFBundleGetInfoString</key>
<string></string>
</dict>
</plist>

当我运行测试时,我看到以下输出:

Test Suite 'RestTest' started at 2016-03-03 15:38:49.364
Test Case '-[FoodTrackerTests.RestTest testResolver]' started.
inside doSomething
another thing
// I want to see the result: the placeholder
Test Case '-[FoodTrackerTests.RestTest testResolver]' passed (0.061 seconds).
Test Suite 'RestTest' passed at 2016-03-03 15:38:49.425.
Executed 1 test, with 0 failures (0 unexpected) in 0.061 (0.062) seconds
Test Suite 'Selected tests' passed at 2016-03-03 15:38:49.426.
Executed 1 test, with 0 failures (0 unexpected) in 0.061 (0.063) seconds

为什么结果变量不包含 post.description 值?

最佳答案

正如评论中所建议的,测试似乎在 HTTP 调用返回之前完成(因为 HTTP 请求是异步的),因此在测试运行结束之前您看不到 API 调用的结果。

看看XCAsyncTestCase并将您的测试设置为等待异步回调。

关于ios - 在 iOS 上使用 swift 消费 REST API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35781688/

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