2016-03-18 95 views
0

我遇到問題,請檢查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"; 
    }; 
}] 

回答

0

作爲一個經驗法則,如果您不知道字典關鍵字是否存在,首選可選鏈接。這將防止由於試圖強制使用零值解包optionals而導致的崩潰。

如果你的數據對象是嵌套的字典所有斯威夫特類型,這將是簡單:

if let url = playListVideos_ARRAY[startOfNextIndexForVideos]["snippet"]?["thumbnails"]?["high"]?["url"] { 
    // attempt to create the image using the successfully obtained url here 
} 

要處理你的情況,我會嘗試打破了if let語句轉換成逗號分隔的列表,更換所有as!as?,只有創建圖像的嘗試如果URL關鍵發現:

if let dict = playListVideos_ARRAY[startOfNextIndexForVideos] as? Dictionary<NSObject, AnyObject>, 
    snippet = dict["snippet"] as? Dictionary<NSObject, AnyObject>, 
    thumbnails = snippet["thumbnails"] as? Dictionary<NSObject, AnyObject>, 
    high = thumbnails["high"] as? Dictionary<NSObject, AnyObject>, 
    url = high["url"] as? Dictionary<NSObject, AnyObject> { 
     // create your image with the url here 
    } else { 
     // do your thing with "2222222.." 
    } 
+0

我已經實現了一個解決方案,但我只是想你的,它的工作是相同的,所以我會標記:) – Pangu

0

是像這樣的東西

if([yourarray valueForKey:@"thumbnails"]!=nil){ 
     NSLog("Key is found."); 
    } 
    else{ 
     NSLog("key is not found..playlist not available"); 
    }