如何在iOS Swift 3中使用防护和条件?
我想得到函数的结果并将其设置在另一个变量(let或var)中,然后用如下条件检查它:如何在iOS Swift 3中使用防护和条件?,ios,swift,swift3,conditional-statements,guard,Ios,Swift,Swift3,Conditional Statements,Guard,我想得到函数的结果并将其设置在另一个变量(let或var)中,然后用如下条件检查它: guard galleryArr:Array<UIImage> = fetchGalleryImages() , galleryArr.count != 0 { }else{ } guard galleryArr:Array=fetchgallerymages(),galleryArr.count!=0 { }否则{ } 请告诉我解决此问题的正确方法。尝试以下方法: func do
guard galleryArr:Array<UIImage> = fetchGalleryImages() , galleryArr.count != 0 {
}else{
}
guard galleryArr:Array=fetchgallerymages(),galleryArr.count!=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 ...
}
试试这个:
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 ...
}
Swift 3.0 您可以在以下情况下使用用户防护和where: 但必须是
fetchGalleryImages()
returnoptional
value
guard let galleryArr = fetchGalleryImages(), galleryArr.count > 0 else {
//What ever condition you want to do on fail or simply return
}
Swift 3.0 您可以在以下情况下使用用户防护和where: 但必须是
fetchGalleryImages()
returnoptional
value
guard let galleryArr = fetchGalleryImages(), galleryArr.count > 0 else {
//What ever condition you want to do on fail or simply return
}
fetchGalleryImages()是如何定义的?它返回数组还是可选数组?fetchGalleryImages()是如何定义的?它返回数组还是可选数组?