【问题标题】:Failed to obtain sandbox extension for path Swift playground error无法获取路径 Swift 游乐场错误的沙盒扩展
【发布时间】:2017-10-22 21:19:31
【问题描述】:

我正在尝试使用 JSON 解析来自 API 的数据并收到以下错误:

Failed to obtain sandbox extension for path=/var/folders/4g/g8pv7pms3_n7grf2y7db_dx00000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.testJSON-CB872E50-7C0F-4640-9B40-BA0EFE9BFA44/Library/Caches/com.apple.dt.playground.stub.iOS_Simulator.testJSON-CB872E50-7C0F-4640-9B40-BA0EFE9BFA44.

我的代码如下:

let url = URL(string: "http://engine.hotellook.com/api/v2/static/hotels.json?locationId=895&token=6251c90d5bc52c88b60a38bd84373513")

let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
    print("IN?")
    if error != nil {
        print ("ERROR")
    }
    else {
        print("Check2")
        if data != nil {
            do {
                //Array
                let myJson = try? JSONSerialization.jsonObject(with: data!, options: [])
                if let dictionary = myJson as? [String: Any] {
                    print(dictionary.description)           
                }
            }
        }
    }
}
task.resume()

【问题讨论】:

  • @LeoDabus 是的,我是
  • 一切正常!谢谢!!!
  • 好的,我会的。谢谢!如果需要,您可以添加上述信息作为答案,我会投票并批准它。

标签: json swift xcode api swift-playground


【解决方案1】:

需要导入 PlaygroundSupport 并将当前页面的 needsIndefiniteExecution 设置为 true:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

除了你应该做一些事情来改进你的代码:

URLSession.shared.dataTask(with: url) { (data, response, error) in
    guard let data = data, error == nil else {
        return
    }
    do {
        let dictionary = try JSONSerialization.jsonObject(with: data) as? [String: Any] ?? [:]
        print(dictionary)
    } catch {
        print(error)
    }
}.resume()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2016-01-28
    • 1970-01-01
    • 2022-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多