2017-07-15 36 views
0

我想要得到的函數結果,並將其設置在另一個變量(VAR),然後用這樣的條件檢查:如何在iOS Swift 3中使用警衛和條件?

guard galleryArr:Array<UIImage> = fetchGalleryImages() , galleryArr.count != 0 { 

}else{ 

} 

請告訴我解決這個問題的正確方法。

+1

fetchGalleryImages()是如何定義的?它返回一個數組還是可選的數組? –

回答

2

雨燕3.0

可以用戶後衛和WHERE條件如下圖所示:

但必須是fetchGalleryImages()返回optional值。

guard let galleryArr = fetchGalleryImages(), galleryArr.count > 0 else { 
//What ever condition you want to do on fail or simply return 
} 
0

試試這個:

func doSomething() -> Int? { 
    guard let galleryArr = fetchGalleryImages(), galleryArr.count != 0 else { 
     // you must return from doSomething in here, be it by throwing 
     // a fatalError(), return nil, or some other value to indicate 
     // that the call has failed 
     return nil 
    } 

    // proceed with the function 
    return ... 
}