我观看了Jared Davidson关于使用SwiftJSON解析JSON的视频。我关注了这段视频:https://www.youtube.com/watch?v=_NfijT6mt6A
它正在工作! ...但JSON文件是本地文件。现在我想将此文件移至我的网站!
在尝试使用远程JSON文件之前,这是我的代码(此代码运行良好):
let path: String = NSBundle.mainBundle().pathForResource("Database", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: NSURL(string: path) as NSData!
let readableJSON = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
// Récupère et classe les élèment de la date
let title = readableJSON["dates"][todayDate]["title"].string as String!
为了能够解析存储在我网站上的JSON文件,我更改了这一行:let jsonData = NSData(contentsOfFile: NSURL(string: path) as NSData!
到此:
let jsonData = NSData(contentsOfURL: NSURL(string: "http://mywebsite.com/Database.json")!) as NSData!
AND BOOOMM:错误:(当我运行模拟器时
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
答案 0 :(得分:2)
Transport security has blocked a cleartext HTTP
想知道为什么似乎更容易在SO上提问只是在Google上搜索错误信息。