gpt4 book ai didi

ios - Swift 中的可选类型错误 : fatal error: unexpectedly found nil while unwrapping an Optional value

转载 作者:行者123 更新时间:2023-11-30 14:14:19 25 4
gpt4 key购买 nike

我正在尝试使用 FBSDK v4.x 从 facebook 获取相册。我使用下面的代码,在打印出来后得到 json 格式的结果,但随后它会导致如下错误: ' fatal error :在解包可选值时意外发现 nil'这一行:let graphData = result.valueForKey("data") as! [FBSDKShareAPI] .所以任何人都可以帮我解决这个错误。提前致谢。

func fetchAlbum(){
print(FBSDKAccessToken.currentAccessToken())
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me?fields=albums", parameters: nil);
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if let gotError = error{
print(gotError.description);
}
else{
print("fetched data: \(result)")
if result != nil{
let graphData = result.valueForKey("data") as! [FBSDKShareAPI]
var albums:[AlbumModel] = [AlbumModel]();
for obj:FBSDKShareAPI in graphData{
let desc = obj.description;
println(desc);
let name = obj.valueForKey("name") as! String;
println(name);
if(name == "ETC"){
let test="";
}
let id = obj.valueForKey("id") as! String;
var cover = "";
if let existsCoverPhoto : AnyObject = obj.valueForKey("cover_photo"){
let coverLink = existsCoverPhoto as! String;
cover = "/\(coverLink)/photos";
}
//println(coverLink);
let link = "/\(id)/photos";
let model = AlbumModel(name: name, link: link, cover:cover);
albums.append(model);
}
NSNotificationCenter.defaultCenter().postNotificationName("albumNotification", object: nil, userInfo: ["data":albums]);

}
}

})
}

最佳答案

更好的是,你可以走

if let graphData = result?.valueForKey("data") as? [FBSDKShareAPI] {
//bla
}

它应该为你做所有的 nil 检查

关于ios - Swift 中的可选类型错误 : fatal error: unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com