ios - 使用 indexPath 访问数组内的 UIImage 并创建空的 UIImage 数组元素

标签 ios arrays swift uiimage nsindexpath

我有一个 UIImage 数组。我想用 3 个空数组元素(UIImage)创建它

var newImages = [UIImage]()

第一个问题:我怎样才能得到这样的结果?

let newImages = [emptyUIImage,emptyUIImage,emptyUIImage]

或者有没有办法为 UIImage 数组存储空数组元素,就像我这样存储 NSURL 数组的空 NSURL 一样?

if data.count != 3 {
    let emptyURL = NSURL(string: "")
    data.append(emptyURL!)
}

最佳答案

您可以创建具有固定数量元素的任何类型的数组,然后使用 array[i] = value 设置中的第 i 索引的值你的数组

//instead of passing 10 you could pass, for example, array.count
  var images = [UIImage](count: 10, repeatedValue: nil)


images[0] = //someImage
images[3] = //someImage
images[1] = //someImage

或者你可以让你的数组成为一个可选的 UIImages 数组,就像这样......

var newImages = [UIImage?]()

然后在有空图像的地方追加 nil ...

newImages.append(nil)

关于ios - 使用 indexPath 访问数组内的 UIImage 并创建空的 UIImage 数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36756704/

相关文章:

html - iOS:将大型 HTML 内容拆分为多个水平分页 View

ios - 自定义 UINavigationController 动画

ios - 确定 CATextLayer 中文本框的大小

ios - 将库 mylibrary.a 包含到 Podspec 中

ios - 如何在 collectionView 的单元格之间放置 View ?

c++ - 我已经阅读了很多有关2d数组的信息,但是在分配作业时遇到了麻烦

ios - Swift - 旋转 UITextView 自动调整大小问题

arrays - 随着分配更多的盒装数组,代码变得更慢

javascript - 如何将项目放入按特定键分组的分组数组中

ios - 快速连接反斜杠