2017-04-14 80 views
-1

做的實現代碼如下功能我「出口」的價值觀,我有以下功能:如何在迅速3

public func tableView(_ tableView: UITableView, didSelectRowAt 
    indexPath: IndexPath) 
    { 
    let currentcell = telefonbog[indexPath.row] 
    ref = FIRDatabase.database().reference() 
    ref?.child("medarbejder").child(currentcell).observeSingleEvent(of: 
    .value, with: { (snapshot) in 
     let value = snapshot.value as? NSDictionary 
     let Mail = (value?["Mail"] as? String)! 
     let Mobil = (value?["Mobil"] as? String)! 
//  self.test.text = Mail 
     self.performSegue(withIdentifier: "kontakt", sender: self) 
     }) 
    } 

我看到它的琴絃郵件和美孚沒有「出口」出這個功能的。 我的問題是我需要在下一個窗口中使用這兩個字符串。但他們是空的?

+0

正確的,但不能得到它工作。 我現在已經試過了,知道它可能是非常基本的,但希望有人能幫助我。 試圖從函數直接填充另一個屏幕,但得到一個失敗 試圖返回,但不會工作。 現在卡住了 –

+0

也嘗試使類var的Mail_和Mobil_,並仍然工作裏面的功能,但他們沒有得到更新外功能 –

回答

0

你定義郵件和美孚爲當地變量,將過期,一旦你離開函數(或關閉在這種情況下)是不確定的 - 這是正常的方式變量斯威夫特表現(或幾乎大多數語言) 。

如果你想保持變量存在,它們需要在函數外部定義爲一個類變量或者將它們作爲參數傳遞給另一個函數。

var mail = "" 
var mobil = "" 

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
    let currentcell = telefonbog[indexPath.row] 
    ref = FIRDatabase.database().reference() 
    ref?.child("medarbejder").child(currentcell).observeSingleEvent(of: 
          .value, with: { (snapshot) in 
     let value = snapshot.value as? NSDictionary 
     self.mail = (value?["Mail"] as? String)! 
     self.mobil = (value?["Mobil"] as? String)! 
     self.performSegue(withIdentifier: "kontakt", sender: self) 
     }) 
    } 

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
    let currentcell = telefonbog[indexPath.row] 
    ref = FIRDatabase.database().reference() 
    ref?.child("medarbejder").child(currentcell).observeSingleEvent(of: 
          .value, with: { (snapshot) in 
     let value = snapshot.value as? NSDictionary 
     let mail = (value?["Mail"] as? String)! 
     let mobil = (value?["Mobil"] as? String)! 
     self.doSegue(theMail: mail, theMobile: mobile) 
    }) 
    }  

func doSegue (theMail: String, theMobile: String) { 
    //do something with mail and mobile var 
    self.performSegue(withIdentifier: "kontakt", sender: self) 
{ 
+0

嗨Jay 謝謝你花時間看看我的問題。 其實我試過你沒有。 1回答問我的問題。 我不知道我哪裏出了問題,但即使有班級變異,他們重置後,我離開功能。 我試圖做的是在Mail和Mobil的「kontakt」中填充標籤,如果我嘗試直接從func Tableview中獲取錯誤,我的代碼現在看起來像:(請參閱下面的答案) –

0

這是也是我的第一個猜測。但低於dosent的代碼中,func中的斷點我表明瓦爾設置正確,但斷點在退出之後的FUNC顯示它去右後衛「」

var Mail = "" 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{ 
    let currentcell = telefonbog[indexPath.row] 
    ref = FIRDatabase.database().reference() 
    ref?.child("medarbejder").child(currentcell).observeSingleEvent(of: .value, with: { (snapshot) in 
     let value = snapshot.value as? NSDictionary 
     self.Mail = (value?["Mail"] as? String)! 
//  let Mobil = (value?["Mobil"] as? String)! 
     self.performSegue(withIdentifier: "kontakt", sender: self) 
    }) 
} 
+0

您好幾種東西結合在一起這並不需要合併。如果你想設置var Mail_,那麼你可以在閉包中做到這一點,沒有理由將它傳遞給一個函數,然後設置它。這就是說,如果你有一個類var,它會'堅持',一旦你離開一個函數改變它。在引用類級別變量時,通常使用self.var_name ...因此在這種情況下,它將是self.Mail_ = theMail。但是,再次,沒有理由將它傳遞給一個函數,然後分配它,只是在閉包內分配它。 – Jay

+0

再次嗨 只是試圖糾正我的回答上面 再次感謝您花時間回覆 –