Swift 如何限制按时间传递的按钮单击

Swift 如何限制按时间传递的按钮单击,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我需要一个按钮,用户点击,让每个人都知道他们正在参加一个位置,但我只希望它能够被点击多达3次每24小时取决于按钮的内容,因为有一个每个单元格一个按钮的tableView 我没有尝试过任何东西,因为我从来没有在斯威夫特使用过时间,这对我来说还是新鲜事 @IBAction func myButtonClicked(_ sender: Any) { DataService.ds.REF_BARS.child(imageTitleLabel.text!).child("GoingCount").

我需要一个按钮,用户点击,让每个人都知道他们正在参加一个位置,但我只希望它能够被点击多达3次每24小时取决于按钮的内容,因为有一个每个单元格一个按钮的tableView

我没有尝试过任何东西,因为我从来没有在斯威夫特使用过时间,这对我来说还是新鲜事

@IBAction func myButtonClicked(_ sender: Any) {
    DataService.ds.REF_BARS.child(imageTitleLabel.text!).child("GoingCount").observeSingleEvent(of: .value, with: { (snapshot) in
        print(snapshot.value as! String)
        self.countString = snapshot.value as! String
        self.countInt = Int(self.countString)! + 1
        DataService.ds.REF_BARS.child(self.imageTitleLabel.text!).updateChildValues(["GoingCount": String(self.countInt)])
    })
}

如果您可以在该按钮内或周围添加用户单击的前置或后置条件,则可以对1个按钮执行此操作

if let storedDate = UserDefaults.standard.object(forKey:"StoredDate") as? Date {

      let toDate = Calendar.current.date(byAdding: .day, value:1, to:storedDate)
      let cliks = UserDefaults.standard.integer(forKey:"NumOfClicks")

      if Date() <= toDate { 
          if cliks < 3 {
                // do what you need and increase NumOfClicks by 1
          }
          else {
             // no more clicks this day
          }
      }
      else { 
         // date exceed may be reset the date and cliks to 1
      }
}
else {
     // first attempt 
     UserDefaults.standard.set(Date(),forKey:"StoredDate")
     UserDefaults.standard.set(1,forKey:"NumOfClicks")
     // do what you need once from here 
}

对于数组处理,您可以将上面的每个键都视为一个数组,在像[Date]/[Int]

这样的存储/检索中,您可以对1个按钮执行此操作

if let storedDate = UserDefaults.standard.object(forKey:"StoredDate") as? Date {

      let toDate = Calendar.current.date(byAdding: .day, value:1, to:storedDate)
      let cliks = UserDefaults.standard.integer(forKey:"NumOfClicks")

      if Date() <= toDate { 
          if cliks < 3 {
                // do what you need and increase NumOfClicks by 1
          }
          else {
             // no more clicks this day
          }
      }
      else { 
         // date exceed may be reset the date and cliks to 1
      }
}
else {
     // first attempt 
     UserDefaults.standard.set(Date(),forKey:"StoredDate")
     UserDefaults.standard.set(1,forKey:"NumOfClicks")
     // do what you need once from here 
}
对于数组处理,您可以将上面的每个键看作一个数组,在存储/检索时,比如[Date]/[Int]