Swift:创建一个UIImage数组 [英] Swift: Creating an array of UIImage

查看:205
本文介绍了Swift:创建一个UIImage数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Swift,我正在尝试为简单动画创建一个UIImage对象数组。 animationImages 的上下文帮助读取数组必须包含UI图像对象。

Using Swift, I'm trying to create an array of UIImage objects for a simple animation. Contextual help for animationImages reads, "The array must contain UI Image objects."

我试图创建所述数组如下所示,但似乎无法使语法正确:

I've tried to create said array as follows, but can't seem to get the syntax correct:

var logoImages: UIImage[]
logoImages[0] = UIImage(name: "logo.png")

此抛出:
!在初始化之前使用的变量logoImages

This throws: ! Variable logoImages used before being initialized

然后我尝试了

var logoImages = []
logoImages[0] = UIImage(named: "logo.png")

哪个投掷:
!无法分配到此表达式的结果

Which throws: ! Cannot assign to the result of this expression

我在这里检查了文档,但上下文不一样:
https://developer.apple.com/library/content/documentation/Swift/Conceptual /Swift_Programming_Language/CollectionTypes.html

I've checked the docs here, but the context isn't the same: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

推荐答案

你有两个问题(没有正则表达式!)

You have two problems (and without a regex!)

var logoImages: [UIImage] = []

var logoImages: Array<UIImage> = []

var logoImages = [UIImage]()

var logoImages = Array<UIImage>()



2。如果要将新对象添加到数组中,则应使用 Array.append()或一些等效的语法糖:



2. If you want to add new objects to an array, you should use Array.append() or some of the equivalent syntactic sugar:

logoImages.append(UIImage(named: "logo.png")!)

logoImages += [UIImage(named: "logo.png")!]

logoImages += [UIImage(named: "logo.png")!, UIImage(named: "logo2.png")!]

您需要附加到数组,因为(摘录来自文档):

You need to append to the array because (excerpt from docs):


你不能使用下标语法将新项追加到
数组的末尾。如果您尝试使用下标语法为数组现有边界之外的索引检索或设置值
,则
将触发运行时错误。但是,通过将索引与数组的count属性进行比较,可以在使用之前检查索引是否有效
。当count为0(意味着数组为空)时,
除外,数组中最大的有效索引
将始终为count-1,因为数组的索引是
零。

You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error. However, you can check that an index is valid before using it, by comparing it to the array’s count property. Except when count is 0 (meaning the array is empty), the largest valid index in an array will always be count - 1, because arrays are indexed from zero.

当然,你可以随时简化它:

Of course you could always simplify it when possible:

var logoImage: [UIImage] = [
    UIImage(named: "logo1.png")!,
    UIImage(named: "logo2.png")!
]

编辑请注意,UIImage现在有一个< a href =https://developer.apple.com/swift/blog/?id=17>failable初始化程序,这意味着它返回一个可选项。我已更新所有代码位以反映此更改以及对数组语法的更改。

edit: Note that UIImage now has a "failable" initializer, meaning that it returns an optional. I've updated all the bits of code to reflect this change as well as changes to the array syntax.

这篇关于Swift:创建一个UIImage数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆