簡體   English   中英

使用 Swift 2 和 PHP 將 JSON 編碼為 Xcode 7

[英]Encode JSON to Xcode 7 with Swift 2 and PHP

我在 let task = session 行上出現錯誤,應用程序運行但是當 json 嘗試加載應用程序時停止並在控制台中顯示致命錯誤:

在解開 Optional 值時意外發現 nil

我只是想顯示我服務器上的 JSON,當我更新到 Xcode 7 時就會出現這個問題

class ViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    let jsonUrl = "{http://test.yozzibeens.com/AppTesting/JsonArray.php}"
    
    let session = NSURLSession.sharedSession()
    let shotsUrl = NSURL(string: jsonUrl)
    
    
    let task = session.dataTaskWithURL(shotsUrl!)
        {
        (data, response, error) -> Void in
        
        do {
            let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
            
            print(String(jsonData["ip"]!))
        } catch _ {
            // Error
        }
    }
    
    task.resume()
    
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

你需要逃避花括號:

let jsonUrl = "{http://test.yozzibeens.com/AppTesting/JsonArray.php}"
let escapeJSONURL = jsonUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

let shotsUrl = NSURL(string: escapeJSONURL)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM