尝试将图像添加到阵列,然后需要将四个阵列合并到一个阵列。 我想做的是:
var arrFiltersImagesList : NSMutableArray = []
var arrRoomInfoImages = Array<UIImage>()
var arrPropertyInfoImages = Array<UIImage>()
var arrAvailabilityImages = Array<UIImage>()
var arrPreferencesImages = Array<UIImage>()
arrRoomInfoImages += [UIImage(named: "ic_roomType_small.png")!, UIImage(named: "ic_furnished_small.png")!, UIImage(named: "ic_numberBedrooms_small.png")!]
arrPropertyInfoImages += [UIImage(named: "ic_small.png")!, UIImage(named: "ic_propertyType_small.png")!, UIImage(named: "ic_numberBedrooms_small.png")!, UIImage(named: "ic_numberBathrooms_small.png")!, UIImage(named: "ic_amenities Copy_small.png")!]
arrAvailabilityImages += [UIImage(named: "ic_roomType_small.png")!, UIImage(named: "ic_furnished_small.png")!, UIImage(named: "ic_ensuite_small.png")!]
arrPreferencesImages += [UIImage(named: "ic_roomType_small.png")!, UIImage(named: "ic_furnished_small.png")!, UIImage(named: "ic_ensuite_small.png")!]
arrFiltersImagesList.addObject(arrRoomInfoImages)
arrFiltersImagesList.addObject(arrPropertyInfoImages)
arrFiltersImagesList.addObject(arrAvailabilityImages)
arrFiltersImagesList.addObject(arrPreferencesImages)
遵循此link,但根本不起作用 输出很糟糕。请指导正确的方法。 感谢
答案 0 :(得分:3)
我会通过将数组构建为let
变量而不是var
变量来实现,利用Swift推断类型的能力:
import UIKit
let arrRoomInfoImages = [
UIImage(named: "ic_roomType_small.png"),
UIImage(named: "ic_furnished_small.png"),
UIImage(named: "ic_numberBedrooms_small.png")].flatMap { $0 }
let arrPropertyInfoImages = [
UIImage(named: "ic_small.png"),
UIImage(named: "ic_propertyType_small.png"),
UIImage(named: "ic_numberBedrooms_small.png"),
UIImage(named: "ic_numberBathrooms_small.png"),
UIImage(named: "ic_amenities Copy_small.png")].flatMap { $0 }
let arrAvailabilityImages = [
UIImage(named: "ic_roomType_small.png"),
UIImage(named: "ic_furnished_small.png"),
UIImage(named: "ic_ensuite_small.png")].flatMap { $0 }
let arrPreferencesImages = [
UIImage(named: "ic_roomType_small.png"),
UIImage(named: "ic_furnished_small.png"),
UIImage(named: "ic_ensuite_small.png")].flatMap { $0 }
let arrFiltersImagesList = [
arrRoomInfoImages,
arrPropertyInfoImages,
arrAvailabilityImages,
arrPreferencesImages]
请注意,我没有强制解包init
方法的返回值。相反,我使用flatMap
来剔除数组中的任何nil
值。这是一种在加载图像时处理错误的快捷方式,您可能希望执行更复杂的操作以确保正确处理任何未加载的图像。