【问题标题】:How do I "export" values from tableview function in swift 3 [duplicate]如何在swift 3中从tableview函数“导出”值[重复]
【发布时间】:2017-04-14 20:08:05
【问题描述】:

我有以下功能:

   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)
       })
    }

我看到字符串 Mail 和 Mobil 并没有从这个函数中“导出”。 我的问题是我需要在下一个窗口中使用这两个字符串。但它们是空的?

【问题讨论】:

  • 正确,但无法正常工作。我现在已经尝试了所有,知道这可能是非常基本的,但希望有人可以帮助我。尝试直接从函数填充另一个屏幕,但失败尝试返回,但不起作用。现在有点卡住了
  • 还尝试制作 Mail_ 和 Mobil_ 的类 var,并且仍然可以在函数内部工作,但它们不会在函数外部得到更新

标签: ios swift uitableview firebase segue


【解决方案1】:

您将 Mail 和 Mobil 定义为 local 变量,一旦您离开函数(或在本例中为闭包),这些变量将过期并变为未定义 - 这是变量在 Swift 中的正常行为方式(或漂亮大多数语言)。

如果您想让变量保持活动状态,则需要在函数外部将它们定义为类变量,或者将它们作为参数传递给另一个函数。

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)
{

【讨论】:

  • 嗨 Jay 感谢您抽出宝贵的时间来查看我的问题。实际上我试过你的不。问我的问题之前有 1 个答案。我不知道我哪里出错了,但即使使用类变量,一旦我离开函数,它们也会重置。我想做的是用Mail和Mobil填充“kontakt”中的标签,如果我直接从func Tableview尝试我会出错,我的代码现在看起来像:(请参阅下面的答案)
【解决方案2】:

这也是我的第一个猜测。但是下面的代码 dosent,func 内的断点显示 Var 设置正确,但是退出 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)
    })
}

【讨论】:

  • 你结合了一些并不真正需要结合的东西。如果你想设置 var Mail_ 那么你可以在闭包中做到这一点,没有理由将它传递给一个函数然后设置它。话虽如此,如果你有一个类 var,一旦你离开一个改变它的函数,它就会“粘住”。当引用类级别的变量时,您通常使用 self.var_name... 所以在这种情况下它将是 self.Mail_ = theMail。但是同样,没有理由将它传递给一个函数然后分配它,只需在闭包中分配它。
  • 您好,再次尝试更正我上面的回答,再次感谢您抽出宝贵时间回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多