Swift: Creating an Array with a Default Value of distinct object instances(Swift:创建具有不同对象实例默认值的数组)
问题描述
我注意到在使用默认值创建数组中有些奇怪(和危险的恕我直言)行为.如Swift 2.1:集合类型
I noticed a bit weird (and dangerous IMHO) behavoir in Creating an Array with a Default Value. As stated in Swift 2.1: Collection Types
Swift 的 Array 类型还提供了一个初始化器,用于创建一个特定大小的数组,其所有值都设置为相同的默认值.您将要添加到新数组中的项目数(称为计数)和适当类型的默认值(称为重复值)传递给此初始化程序:
Swift’s Array type also provides an initializer for creating an array of a certain size with all of its values set to the same default value. You pass this initializer the number of items to be added to the new array (called count) and a default value of the appropriate type (called repeatedValue):
重点是:默认值相同;为了理解它是如何工作的,我尝试创建这个示例类的元素数组
The point is: same default value; in order to understand how it work, I tried to create an array of elements of this example class
class User {
private struct Shared {
static var sequence: Int = 0
}
var id: Int
var thinkTime: NSTimeInterval // typealias di Double
init (thinkTime: NSTimeInterval) {
User.Shared.sequence = User.Shared.sequence+1
id = User.Shared.sequence
self.thinkTime = thinkTime
}
}
还有这个测试代码:
let howManyUsers: Int = 3
var users = [User](count: howManyUsers, repeatedValue:User(thinkTime: 10.0))
let u2: User = User(thinkTime: 10)
let u3: User = User(thinkTime: 10)
users.append(u2)
users.append(u3)
users[1].thinkTime = 20
users[3].thinkTime = 30
for u in users {
print("User id:(u.id) thinktime:(u.thinkTime)")
}
给予:
User id:1 thinktime:20.0
User id:1 thinktime:20.0
User id:1 thinktime:20.0
User id:2 thinktime:30.0
User id:3 thinktime:10.0
用要添加到新数组的项目数和适当类型的默认值来明确证明初始化程序是:相同的对象实例
that definitively proof the initializer with the number of items to be added to the new array and a default value of the appropriate type are: the same object instance
这是获取 distinct object instances 数组的方法,尽可能简洁和智能,使用相同的默认值(不是相同的实例,而是使用相同的默认值)?
Which is the way, as concise and smart as possible, to obtain a array of distinct object instances , instatiated with the same default value ( not the same instance but a number of instances initialized with the same default value ) ?
推荐答案
类是引用类型,因此——正如你所注意到的——所有数组中的元素
Classes are reference types, therefore – as you noticed – all array elements in
var users = [User](count: howManyUsers, repeatedValue:User(thinkTime: 10.0))
引用同一个对象实例(先创建后作为参数传递给数组初始值设定项).
reference the same object instance (which is created first and then passed as an argument to the array initializer).
对于 struct
类型,您会得到不同的结果.
For a struct
type you would get a different result.
一个可能的解决方案:
var users = (0 ..< howManyUsers).map { _ in User(thinkTime: 10.0) }
这里,为每个数组索引创建一个 User
实例.
Here, a User
instance is created for each of the array indices.
如果你经常需要,那么你可以定义一个数组 init采用自动关闭"参数的方法:
If you need that frequently then you could define an array init method which takes an "autoclosure" parameter:
extension Array {
public init(count: Int, @autoclosure elementCreator: () -> Element) {
self = (0 ..< count).map { _ in elementCreator() }
}
}
var users = Array(count: howManyUsers, elementCreator: User(thinkTime: 10.0) )
现在第二个参数 User(thinkTime: 10.0)
由编译成一个闭包,每个闭包都会执行数组索引.
Now the second argument User(thinkTime: 10.0)
is wrapped by the
compiler into a closure, and the closure is executed for each
array index.
Swift 3 更新:
extension Array {
public init(count: Int, elementCreator: @autoclosure () -> Element) {
self = (0 ..< count).map { _ in elementCreator() }
}
}
这篇关于Swift:创建具有不同对象实例默认值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift:创建具有不同对象实例默认值的数组
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01
- URL编码Swift iOS 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01