2014-08-28 78 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