【发布时间】:2016-05-25 06:45:52
【问题描述】:
我正在尝试将我的 JSON 数据传递到我的领域数据库,但我一直被抛出此错误
致命错误:在展开可选值时意外发现 nil
user.name = (result["name"]?.string)!,但是当我执行print(result) 时,我能够得到这个输出
Particulars {
"name" : "Jonny Walker",
"api_token" : "qwertyuiop1234567890",
"profile_picture" : "http:default_profile_picture.jpg",
"id" : 10,
"email" : "jwalker@gmail.com"
"username" : "jonny"
}
这是我的代码:
Alamofire.request(.POST, Data.loginEndpoint, parameters: parameters)
.responseObject { (response: Response<Particulars, NSError>) in
if let result = response.result.value
{
do{
print(Realm.Configuration.defaultConfiguration.fileURL)
print(result)
let user = Particulars()
let realm = try Realm()
user.name = (result["name"]?.string)!
user.apiToken = (result["api_token"]?.string)!
try realm.write() {
realm.add(user, update: true)
}
}
catch let err as NSError {
print("Error with realm: " + err.localizedDescription)
}
}
else
{
print("JSON data is nil. 123")
}
}
【问题讨论】:
标签: ios swift2 realm optional word-wrap