【发布时间】:2017-02-15 06:56:10
【问题描述】:
下面是我在 ObjC 中的代码 sn-p
NSDictionary *json;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"realstories" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
我尝试过以这种方式使用它的 Siwft 等价物:
var json = [AnyHashable:Any]()
let filePath: String? = Bundle.main.path(forResource: "realstories", ofType: "json")
let data = NSData(contentsOfFile:filePath!)
json = ((NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as! NSDictionary) as! [AnyHashable:Any])
但我陷入了错误:
unexpectedly found nil while unwrapping an Optional value
尝试阅读它Here。但是,无法解决错误!
【问题讨论】:
-
你在哪一行面对这个
-
第 -4 行(带有 NSKeyedUnarchiver 的 json)