在Swift中初始化固定大小的数组时输入错误

时间:2022-07-01 02:41:38

I'm in the middle of implementing Hash Table using a linked list. There are total 3 classes.

我正在使用链表实现哈希表。共有3个班级。

    class HashNode<Key: Hashable, Value> {
        var key: Key
        var value: Value
        var next: HashNode?

        init(key: Key, value: Value) {
            self.key = key
            self.value = value
        }
    }

    class HashTableBucket<Key: Hashable, Value> {
        typealias Node = HashNode<Key, Value>
        var head: Node?
        var tail: Node?

        func addNode(newNode: Node) {
            //code
        }

        func findNode(key: Key) -> Node?{
             //code
        }
}

struct HashTable<Key: Hashable, Value> {
    private typealias Bucket = HashTableBucket<Key, Value>
    private var buckets: [Bucket]

    private(set) public var count = 0
    private(set) public var capacity = 0

    init(capacity: Int) {
        assert(capacity > 0)
        buckets = Array<Bucket>(repeating: [], count: capacity)
    }

    //other code
}

When I initialize the HashTable instance, I want to make a fixed size array which is a type of Bucket(or HashTableBucket) with nil values. I essentially want to make [[], [], [], [], []] I'm getting an error on the line buckets = Array<Bucket>(repeating: [], count: capacity). The error says,

当我初始化HashTable实例时,我想制作一个固定大小的数组,这是一种具有nil值的Bucket(或HashTableBucket)类型。我本质上想要[[],[],[],[],[]]我在行buckets = Array (重复:[],count:capacity)上收到错误。错误说,

Playground execution failed: error: HashTable.xcplaygroundpage:163:19: error: cannot invoke initializer for type 'Array<HashTableBucket<Key, Value>>' with an argument list of type '(repeating: [Any], count: Int)'
        buckets = Array<Bucket>(repeating: [], count: capacity)
                  ^

HashTable.xcplaygroundpage:163:19: note: expected an argument list of type '(repeating: Element, count: Int)'
        buckets = Array<Bucket>(repeating: [], count: capacity)

What am I doing wrong here?

我在这做错了什么?

1 个解决方案

#1


1  

The repeating: argument is an instance of the array's element type, e.g.

repeat:参数是数组元素类型的一个实例,例如

buckets = Array<Bucket>(repeating: Bucket(), count: capacity)

to create an array of Buckets. This can be simplified to

创建一个Buckets数组。这可以简化为

buckets = Array(repeating: Bucket(), count: capacity)

due to automatic type inference.

由于自动类型推断。

However, (as you noticed in the meantime :) Bucket is a class and this will create an array with multiple references to the same object instance, which is not what you intend. A possible solution is

但是,(正如您在此期间注意到的那样)Bucket是一个类,这将创建一个具有对同一对象实例的多个引用的数组,这不是您想要的。可能的解决方案是

buckets = (0..<capacity).map { _ in Bucket() }

for more, see Swift: Creating an Array with a Default Value of distinct object instances.

有关更多信息,请参阅Swift:使用不同对象实例的默认值创建数组。

#1


1  

The repeating: argument is an instance of the array's element type, e.g.

repeat:参数是数组元素类型的一个实例,例如

buckets = Array<Bucket>(repeating: Bucket(), count: capacity)

to create an array of Buckets. This can be simplified to

创建一个Buckets数组。这可以简化为

buckets = Array(repeating: Bucket(), count: capacity)

due to automatic type inference.

由于自动类型推断。

However, (as you noticed in the meantime :) Bucket is a class and this will create an array with multiple references to the same object instance, which is not what you intend. A possible solution is

但是,(正如您在此期间注意到的那样)Bucket是一个类,这将创建一个具有对同一对象实例的多个引用的数组,这不是您想要的。可能的解决方案是

buckets = (0..<capacity).map { _ in Bucket() }

for more, see Swift: Creating an Array with a Default Value of distinct object instances.

有关更多信息,请参阅Swift:使用不同对象实例的默认值创建数组。