Lampros Tzanetos
Lampros Tzanetos

Reputation: 323

Failed to obtain sandbox extension for path Swift playground error

I am trying to parse data from an API using JSON and get the following error:

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.

My code is as follows:

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()

Upvotes: 2

Views: 1922

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236478

You need to import PlaygroundSupport and set the current page needsIndefiniteExecution to true:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

Besides that there are a few things you should do to improve your code:

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()

Upvotes: 1

Related Questions