Search code examples
iosswiftswift3conditional-statementsguard

How to using guard and condition in iOS Swift 3?


I wanna to get result of function and set it in another variable(let or var) and then check it with a condition like this:

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

}else{

}  

Please tell me the right way to fix this.


Solution

  • Swift 3.0

    you can user guard and where condition like below:

    But it must be that fetchGalleryImages() return optional value.

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