ios Swift:创建一个 UIImage 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24172180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:50:18  来源:igfitidea点击:

Swift: Creating an array of UIImage

iosarraysuiimageswift

提问by kmiklas

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

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

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")

This throws: ! Variable logoImages used before being initialized

这抛出:!初始化前使用的可变 logoImages

Then I tried

然后我试过了

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

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

哪个抛出:!不能赋值给这个表达式的结果

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

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

回答by Jiaaro

You have two problems (and without a regex!)

您有两个问题(并且没有正则表达式!)

1. You aren't creating an array. You need to do:

1. 您不是在创建数组。你需要做:

var logoImages: [UIImage] = []

or

或者

var logoImages: Array<UIImage> = []

or

或者

var logoImages = [UIImage]()

or

或者

var logoImages = Array<UIImage>()

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

2. 如果你想向数组中添加新对象,你应该使用Array.append()或 一些等效的语法糖:

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

or

或者

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

or

或者

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

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

您需要附加到数组,因为(摘自docs):

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.

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

Of course you could always simplify it when possible:

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

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

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.

编辑:请注意, UIImage 现在有一个“失败的”初始化程序,这意味着它返回一个可选的。我已经更新了所有代码以反映此更改以及对数组语法的更改。

回答by fqdn

You are declaring the type for logoImages but not creating an instance of that type.

您正在声明 logoImages 的类型,但未创建该类型的实例。

Use var logoImages = UIImage[]()which will create a new array for you.

使用var logoImages = UIImage[]()这将为您创建一个新数组。

...and then after creating a new empty Array instance, as described in the answer by @Jiaaro you can't use subscripting to add to an empty array

...然后在创建一个新的空数组实例之后,如@Jiaaro 的回答中所述,您不能使用下标添加到空数组

回答by Marco Speziali

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