2017-08-01 24 views
0

我希望數組的結構作爲參數在我的函數中進行更改,如果我想要字符串來更改我把文本:字符串,但是當我想測試時要放什麼:ArrayOfStruct作爲參數?如何獲取多個結構數組的一個函數?

func prizeCsGoArraySetup(arrayToAppend: here i want my struct){ 
    if UserDefaults.standard.object(forKey: "prizeArray") != nil { 
     let demo = UserDefaults.standard.object(forKey: "prizeArray") as! NSDictionary 
     if let priArray = demo.value(forKey: "csgo") as? NSArray{ 
      for prizeItem in priArray { 
       if let prizeVC = prizeItem as? NSDictionary{ 
        let newElement : prize = prize(category: prizeVC.value(forKey: "category") as! String, name: prizeVC.value(forKey: "name") as! String, price: prizeVC.value(forKey: "price") as! Int, image: prizeVC.value(forKey: "image") as! String) 
        arrayToAppend.append(newElement) 

我想爲另外7個數組添加可變結構數組,怎麼做? 我不想整個函數寫7次。

+0

很難說出你在問什麼。你能否將你的代碼減少到能夠說明你的問題的MWE? – Raphael

回答

0

我找到答案,我需要在使用前INOUT。

func prizeArraySetup(arrayToAppend: inout [prize], forKeyText: String) 
0

它很容易

func prizeCsGoArraySetup(arrayToAppend: [MyStruct]) 
+0

根據問題中「可變」的含義,可能在這裏有一個「inout」。 – Raphael

+0

是的,它在我的情況下是缺少的。 –

+0

如果我會這樣做,你會得到錯誤,不能對不可變值使用變異成員 –

相關問題