Swift 3 – some snippets..

  • if let paypal = user.paypal {
    // paypal != nil
    } else {
    // paypal == nil
    }
  • guard let paypal = user.paypal else {
    // paypal == nil
    return
    }
  • Handler :
    • VC 1: (CashoutContentVC)

      var loadTotalBalanceHandler: (()->())?  

      if (condition) {

                           if let callback = self.loadTotalBalanceHandler {

                              callback()

                         }
      }

    • VC 2:

      let cashoutVC = storyboard.instantiateViewController(withIdentifier: “CASH OUT”) as! CashoutContentVC

              cashoutVC.loadTotalBalanceHandler = {[weak self] in

                  self?.loadTotalBalance() //things need to do when callback

              }

      //example 2 :  pass a Double from vc1 -> vc2 

    • VC1:
      var
      ratingScoreHandler: ((_ score: Double) -> ())?

      if (condition) {

                     if let callback = self.ratingScoreHandler {

                          callback(rating)

                      }
      }

    • VC 2:

      let ratingVC = RatingVC(nibName: “RatingVC”, bundle: nil)

      ratingVC.ratingScoreHandler = { [weak self] (score) in

                  if let strongSelf = self {

                      strongSelf.ratingScore = Int(score) //things need to do when callback

                  }

              }