0
我想要得到的函數結果,並將其設置在另一個變量(讓或VAR),然後用這樣的條件檢查:如何在iOS Swift 3中使用警衛和條件?
guard galleryArr:Array<UIImage> = fetchGalleryImages() , galleryArr.count != 0 {
}else{
}
請告訴我解決這個問題的正確方法。
我想要得到的函數結果,並將其設置在另一個變量(讓或VAR),然後用這樣的條件檢查:如何在iOS Swift 3中使用警衛和條件?
guard galleryArr:Array<UIImage> = fetchGalleryImages() , galleryArr.count != 0 {
}else{
}
請告訴我解決這個問題的正確方法。
雨燕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
}
試試這個:
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 ...
}
fetchGalleryImages()是如何定義的?它返回一個數組還是可選的數組? –