2014-08-28 109 views
0

大家好,真正的新手问题在这里。将元素附加到数组

我有一个这样的数组:

var daysInMonth = Array<([MyCustomClass], NSDate)>() 

我怎么能一个元素追加到这一点?

我很难这样做。尝试是这样的:

daysInMonth.append([MyCustomClass](), someDate) 

daysInMonth.append( ([MyCustomClass](), someDate) ) 

将无法​​正常工作(我想添加一个空数组最初的上述类型MyCustomClass的,以及一些日期,我有),但这些失败(错误缺少调用参数#2)

对我缺乏语法的任何想法?

谢谢!

+0

所以你试图一次附加2个单独的值? – 2014-08-28 03:32:19

+1

我不确定你想要达到什么效果,但它有点“代码味”。而不是使用一个类型数组,可能更好的设计是定义一个新的结构,该结构包含一个'MyCustomClass'和一个NSDate的数组,然后声明这些结构的数组 – Paulw11 2014-08-28 03:42:18

回答

2

它看起来像一个迅速的错误给我。 swift编译器无法正确解析“((...))”作为将元组传递给函数。

如果我将append操作分解为两个语句,它将起作用。

var daysInMonth = Array<([MyCustomClass], NSDate)>() 

let data = ([MyCustomClass()], NSDate()) // assuming MyCustomClass init() taks no parameter 

daysInMonth.append(data) 

:这是你的问题,这是不正确[MyCustomClass]()

+0

哪个更好,因为它更清楚,每个陈述都在做一件事情。但是使用新的语法会更好。 – zaph 2014-08-28 04:16:13

+0

@Zaph Yeah,我期望他们是相等的。所以我对这个'错误'感到有些吃惊(如果它确实是一个) – 2014-08-28 04:24:57

+0

谢谢安东尼。欣赏你的时间。 – NullHypothesis 2014-08-29 00:59:07

1

尝试宣告你的阵列使用较新的Array语法来代替:

var daysInMonth = [([MyCustomClass], NSDate)]() 

然后,这个工程:

daysInMonth.append(([MyCustomClass](), NSDate())) 
+0

,这要感谢Mike这么做了! – NullHypothesis 2014-08-29 00:58:35