2016-11-12 61 views
0

我想將兩個值附加到我的變量countryAndCode。 我得到的錯誤:Extra argument 'code' in call這對我沒有任何意義,因爲countryAndCode取兩個值:國家和代碼?調用swift中的額外參數

我做錯了什麼?

這裏是我的代碼:

func startObersvingDB() { 
     FIRDatabase.database().reference().child("UserInformation").observeEventType(.ChildAdded, withBlock: { snapshot in 

      if let title = snapshot.value!["usersUID"] as? String { 
       self.tableData.append(title) 


       if let name = snapshot.value!["userName"] as! String? { 

        self.tableDataNames.append(name) 



        for i in 0...self.tableData.count { 
         var countryAndCode: [(country:String, code: String)] 
         countryAndCode.append(self.tableData[i], code: self.tableDataNames[i]) 
        } 



        self.tableView.reloadData() 

       } else { 
        print("Noget virker ikke i startObservingDB") 
       } 



      } 
     }) 


    } 

如果是相關的;我正在嘗試創建一個顯示用戶名的搜索欄,同時也顯示他們的圖片(這就是爲什麼我需要tableData中的id)。

回答

1

你只需要將你傳入的參數封裝在括號中。

更改此: countryAndCode.append(self.tableData[i], code: self.tableDataNames[i])

到: countryAndCode.append((self.tableData[i], code: self.tableDataNames[i]))

你需要這樣做的原因是因爲你與[(country:String, code: String)]用括號封裝陣列內部的爭論宣告你的陣列,所以你需要將數據附加到它時執行相同操作。

+0

你知道我爲什麼會得到這個錯誤:'index out of range' at this code ?:'countryAndCode.append((self.tableData [i],code:self.tableDataNames [i]))' –

+0

I認爲它來自'tableData [i]'索引而不是countryAndCode數組 – RyanM