我有一个Array
的Image
链接-
let alamofireSource = [AlamofireSource(urlString: Img1!)!, AlamofireSource(urlString: Img2!)!,
AlamofireSource(urlString: Img3!)!, AlamofireSource(urlString: Img4!)!]
slideshow.setImageInputs(alamofireSource)
有些帖子只有一两张图片,以此类推。所以,有时图像2(例如)为零,在这种情况下,我不希望它被添加到数组中,这可能吗?
最佳答案
你可以试试(斯威夫特4)
let arr = [img1,img2].compactMap{$0}.map{AlamofireSource(urlString:$0)!}
或
let arr = alamofireSource.compactMap{$0}
对于Swift 3
let arr = alamofireSource.flatMap{$0}