gpt4 book ai didi

json - Swift - 解析多个可选 Json 答案

转载 作者:行者123 更新时间:2023-11-30 10:07:16 27 4
gpt4 key购买 nike

下面的代码工作正常,至少在我的几次测试中是这样。问题是我真的不知道我是否涵盖了代码中所有可能的失败,或者这是否是处理多个“if let...”Options 的最佳方法。有没有更好、更实用的方法来做到这一点?该代码只是为了处理我从 Youtube API 获取的数据。

func requestPlaylistTrailers(){
let playlist = self.playlistIdTrailers
self.delegate?.youtubeTrailersWillUpdate()
self.req(playlist) { (myData) -> Void in
var title = ""
var videoId = ""
var thumbnail = ""
if myData != nil{
let data = myData!
let items = data["items"] as! Array<AnyObject>
for item in items {
//print(item)
if let itemDict = item as? Dictionary<NSObject,AnyObject>{
if let snippet = itemDict["snippet"] as? Dictionary<NSObject,AnyObject> {
if let getTitle = snippet["title"] as? String{ title = getTitle}
if let getVideoId = snippet["resourceId"] as? Dictionary<NSObject,AnyObject> {
if let getVideoId2 = getVideoId["videoId"] as? String{ videoId = getVideoId2}
}
if let myThumb = snippet["thumbnails"] as? Dictionary<NSObject,AnyObject>{
if let myHighThumb = myThumb["high"] as? Dictionary<NSObject,AnyObject>{
if let thumbUrl = myHighThumb["url"] as? String{
thumbnail = thumbUrl
}
}
}
}
}
if (thumbnail != "" && videoId != ""){
self.arrayYoutubeTrailers.append(["title": title,"videoId": videoId, "thumbnail": thumbnail])
}
}
self.delegate?.youtubeTrailersDidUpdate(self.arrayYoutubeTrailers)
} else {
self.delegate?.youtubeTrailersFailed()
}
}
}

由于答案可能没有“缩略图”或“标题”,我已经得到了一些类似的答案,我必须检查所有内容,否则应用程序将崩溃。

只是想让您知道,这是答案中的一项

{
contentDetails = {
videoId = "va-0o_xBVnU";
};
etag = "\"DsOZ7qVJA4mxdTxZeNzis6uE6ck/spb02vX6UoYkYSIcBFTCzlPQ3Es\"";
id = "PLegkaUUrMBQZ7KYS4cdBhF5aRmyOFe-zLBqlqoAM_wH4";
kind = "youtube#playlistItem";
snippet = {
channelId = UCi8e0iOVk1fEOogdfu4YgfA;
channelTitle = "Movieclips Trailers";
description = "Subscribe to TRAILERS: http://bit.ly/sxaw6h\nSubscribe to COMING SOON: http://bit.ly/H2vZUn\nLike us on FACEBOOK: http://goo.gl/dHs73\nFollow us on TWITTER: http://bit.ly/1ghOWmt \nMoney Monster Official Trailer #1 (2016) - George Clooney, Julia Roberts Movie HD\n\nIn the taut and tense thriller Money Monster, Lee Gates (George Clooney) is a bombastic TV personality whose popular financial network show has made him the money wiz of Wall Street. But after he hawks a high tech stock that mysteriously crashes, an irate investor (Jack O'Connell) takes Gates, his crew, and his ace producer Patty Fenn (Julia Roberts) hostage live on air. Unfolding in real time, Gates and Fenn must find a way to keep themselves alive while simultaneously uncovering the truth behind a tangle of big money lies.\n\n\nThe Fandango MOVIECLIPS Trailers channel is your destination for the hottest new trailers the second they drop. Whether it's the latest studio release, an indie horror flick, an evocative documentary, or that new RomCom you've been waiting for, the Fandango MOVIECLIPS team is here day and night to make sure all the best new movie trailers are here for you the moment they're released.\n\nIn addition to being the #1 Movie Trailers Channel on YouTube, we deliver amazing and engaging original videos each week. Watch our exclusive Ultimate Trailers, Showdowns, Instant Trailer Reviews, Monthly MashUps, Movie News, and so much more to keep you in the know.\n\nHere at Fandango MOVIECLIPS, we love movies as much as you!";
playlistId = "PLScC8g4bqD47c-qHlsfhGH3j6Bg7jzFy-";
position = 26;
publishedAt = "2016-01-13T17:25:53.000Z";
resourceId = {
kind = "youtube#video";
videoId = "va-0o_xBVnU";
};
thumbnails = {
default = {
height = 90;
url = "https://i.ytimg.com/vi/va-0o_xBVnU/default.jpg";
width = 120;
};
high = {
height = 360;
url = "https://i.ytimg.com/vi/va-0o_xBVnU/hqdefault.jpg";
width = 480;
};
maxres = {
height = 720;
url = "https://i.ytimg.com/vi/va-0o_xBVnU/maxresdefault.jpg";
width = 1280;
};
medium = {
height = 180;
url = "https://i.ytimg.com/vi/va-0o_xBVnU/mqdefault.jpg";
width = 320;
};
standard = {
height = 480;
url = "https://i.ytimg.com/vi/va-0o_xBVnU/sddefault.jpg";
width = 640;
};
};
title = "Money Monster Official Trailer #1 (2016) - George Clooney, Julia Roberts Movie HD";
};
}

提前致谢。

最佳答案

您可以将“if let”代码链接在一起,这样至少它看起来不会那么难看。然后而不是有;

if let ...
if let ...
if let ... etc

你刚刚

if let ...,
let ...,
let ... {
// All good, do stuff
} else {
// Something went wrong
}

那么你只需要处理另外一种情况。

关于json - Swift - 解析多个可选 Json 答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35352252/

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