我遇到問題,請檢查UIImage
是否爲零,以顯示UIAlertViewController
。如何在Swift中使用YouTube Api v3來正確檢查UIImage是否爲零?
我的應用程序從youtube頻道獲取播放列表。其中一個播放列表中包含的視頻已被YouTube刪除,因此在獲取播放列表數據時不包含「縮略圖」鍵。
這是我已經執行的代碼來檢查是否有縮略圖圖像顯示,否則顯示警報。
print("playlistVideos_ARRAY = \(self.playlistVideos_ARRAY)")
print("startOfNextIndexForVideos = \(self.startOfNextIndexForVideos)")
if let thumbnailImage: UIImage = UIImage(data: NSData(contentsOfURL: NSURL(string: ((((self.playlistVideos_ARRAY[self.startOfNextIndexForVideos] as! Dictionary<NSObject, AnyObject>)["snippet"] as! Dictionary<NSObject, AnyObject>)["thumbnails"] as! Dictionary<NSObject, AnyObject>)["high"] as! Dictionary<NSObject, AnyObject>)["url"] as! String)!)!)!
{
print("111111111")
self.thumbnailOfVideo.append(thumbnailImage)
}
else
{
print("222222222")
let message = UIAlertController(title: "Uh Oh!", message: "There is an issue with this playlist :(", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
message.addAction(defaultAction)
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(message, animated: true, completion: nil)
})
}
但是,它從來沒有輸出222222222
。它總是失敗的權利,如果檢查與以下輸出:
playlistVideos_ARRAY = [{
etag = "\"q5k97EMVGxODeKcDgp8gnMu79wM/Jl892aZsvFYpvjE5ENwTItH9cqQ\"";
id = PLP2q8Ric9B9X6r8L2LA7xRwg0HNnKL3RLaooSkbmjTxo;
kind = "youtube#playlistItem";
snippet = {
channelId = UC5ltMmeC4YFaart1SSXdmAg;
channelTitle = "Viet My Television";
description = "This video is private.";
playlistId = "PLxnnlv22Xcq34TNdsMxEIKvIHE_phd7XK";
position = 0;
publishedAt = "2016-03-16T18:51:39.000Z";
resourceId = {
kind = "youtube#video";
videoId = uPnWMZ1pbSc;
};
title = "Private video";
};
}]
startOfNextIndexForVideos = 0
fatal error: unexpectedly found nil while unwrapping an Optional value
我在做什麼錯誤檢查這方面?謝謝。
編輯: 這裏的輸出我playlistVideos_ARRAY
通過包含與相關視頻的縮略圖還沒有被YouTube刪除一個有效的播放列表的GET請求獲取:
playlistVideos_ARRAY = [{
etag = "\"q5k97EMVGxODeKcDgp8gnMu79wM/U4OVbnjwS2bbU44Q1Hijy6-koAE\"";
id = "PLP2q8Ric9B9VZXfc41HzRjprfOHpOYQd_SbLAuuxZVyw";
kind = "youtube#playlistItem";
snippet = {
channelId = UC5ltMmeC4YFaart1SSXdmAg;
channelTitle = "Viet My Television";
description = "";
playlistId = PLxnnlv22Xcq0HpE242iwHh8IP78esU9sF;
position = 0;
publishedAt = "2016-02-15T23:25:20.000Z";
resourceId = {
kind = "youtube#video";
videoId = mIW5trLZJcM;
};
thumbnails = {
default = {
height = 90;
url = "https://i.ytimg.com/vi/mIW5trLZJcM/default.jpg";
width = 120;
};
high = {
height = 360;
url = "https://i.ytimg.com/vi/mIW5trLZJcM/hqdefault.jpg";
width = 480;
};
medium = {
height = 180;
url = "https://i.ytimg.com/vi/mIW5trLZJcM/mqdefault.jpg";
width = 320;
};
};
title = "talkshow chuong trinh hoi cho tet Binh Than o Miles Square Park";
};
}]
我已經實現了一個解決方案,但我只是想你的,它的工作是相同的,所以我會標記:) – Pangu