创建struct的空数组会给我一个错误

时间:2022-08-01 21:22:52

I'm trying to create an empty array of a struct. Here's my code:

我试图创建一个结构体的空数组。这是我的代码:

struct Item {
    var prop1 : NSDate
    var prop2 : NSDate
}

var myItem = [Item()]

But I get an error:

但我犯了一个错误:

Missing argument for parameter 'prop1' in call.

参数“prop1”在call中缺少参数参数。

What am I doing wrong, and how can I fix it?

我做错了什么,我该如何改正?

1 个解决方案

#1


2  

Item() is trying to create an Item that is empty, but that's not possible since neither prop1 nor prop2 have default values. The automatically generated initializer requires two arguments.

Item()试图创建一个为空的项,但这是不可能的,因为prop1和prop2都没有默认值。自动生成的初始化器需要两个参数。

To create an empty array, you can use one of these:

要创建一个空数组,您可以使用以下其中之一:

var myItem: [Item] = []
var myItem = [Item]()
var myItem = [] as [Item]

#1


2  

Item() is trying to create an Item that is empty, but that's not possible since neither prop1 nor prop2 have default values. The automatically generated initializer requires two arguments.

Item()试图创建一个为空的项,但这是不可能的,因为prop1和prop2都没有默认值。自动生成的初始化器需要两个参数。

To create an empty array, you can use one of these:

要创建一个空数组,您可以使用以下其中之一:

var myItem: [Item] = []
var myItem = [Item]()
var myItem = [] as [Item]