2015-10-17 146 views
-2

追加數組我有一個問題,追加數組是這樣的:在迅速

var medicine = [(String, String?)]() 

從parse SDK我得到兩個數組:

medicinesNames = ["Zyrtec", "medicine1", "medicine2"]
let amountName = ["2 times a day" , "in the morning", "after lunch"]

問題是如何追加我的屬性數組與字符串在這些數組?
我想將它追加到單獨的函數中。 1日的樂趣輸出:
[("Zyrtec", nil), ("medicine1", nil)... ]
而第二個樂趣後,我想要得到的屬性是這樣的:
[("Zyrtec", "2 times a day"), ("Medicine1", "in the morning"),...]

+1

請出示所期望的結果,你有兩個字符串數組和一個可選的元組數組 - 通常情況下,如果您想要將某些東西附加到數組或字典中,可以使用'var'而不是'let'來定義它們。 – luk2302

回答

1

我建議通過兩個函數的東西更簡潔,這樣做:

let medicinesNames = ["Zyrtec", "medicine1", "medicine2"] 
let amountName = ["2 times a day" , "in the morning", "after lunch"] 

let medicine = Array(zip(medicinesNames, amountName)) 

類型的medicine[(String, String)],和值將是:

[("Zyrtec", "2 times a day"), ("medicine1", "in the morning"), ("medicine2", "after lunch")] 
0

創建的元組的兩個獨立數組的數組可以重複的循環來實現

var medicine = [(String, String?)]() 

let medicinesNames = ["Zyrtec", "medicine1", "medicine2"] 
let amountName = ["2 times a day" , "in the morning", "after lunch"] 
assert(medicinesNames.count == amountName.count, "both arrays must contain the same number of items") 
for i in 0..<medicinesNames.count { 
    medicine.append((medicinesNames[i], amountName[i])) 
} 

實際上沒有時間的藥物是不可能的,我建議你聲明數組非可選[(String, String)]