Swift:使用不同对象实例的默认值创建数组 [英] Swift: Creating an Array with a Default Value of distinct object instances

查看:41
本文介绍了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

哪种方式尽可能简洁和智能,获取不同对象实例的数组,使用相同的默认值(不是同一个实例,而是使用相同的默认值)?

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:使用不同对象实例的默认值创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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