/*
* 加载本地文件
*/
// 1.获取文件路径
let path = Bundle.main.path(forResource: "testVideo.mp4", ofType: nil)
// 2.通过文件路径创建NSData
if let jsonPath = path {
let jsonData = NSData(contentsOfFile: jsonPath)
// 带throws的方法需要抛异常
do {
/*
* 有可能发生异常的代码放在这
*/
// 3.序列化 data -> array
/*
* try 和 try! 的区别
* try 发生异常会跳到catch代码中
* try! 发生异常程序会直接crash
*/
let dictArr = try JSONSerialization.jsonObject(with: jsonData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers)
// 4.遍历数组
// 在Swift中遍历数组,必须明确数据的类型 [[String: String]]表示字典里键值都是字符串 [[String]]表示数组里都是字符串
for dict in dictArr as! [[String: String]] {
print(dict)
}
}catch {
// 异常代码放在这
print(error)
}
}
// 检测连接是否存在 不存在报错
// guard let url = URL(string: "http://bos.nj.bpc.baidu.com/tieba-smallvideo/11772_3c435014fb2dd9a5fd56a57cc369f6a0.mp4") else { fatalError("连接错误") }