如何使用静态变量来跟踪增量?

时间:2020-05-09 23:28:12

标签: ios firebase google-cloud-firestore static

我正在使用静态变量来跟踪在Firebase中添加的酒店数量。假设,一开始,静态变量的值为1,然后在firebase中添加数据时,数字增加为2。但是,当再次添加数据并再次加载此视图控制器时,静态变量的值恢复为1,发布的新数据将替换旧数据。我该如何处理?我知道这个非常基本且愚蠢的问题,但有时大脑只是不起作用。下面是代码。

class OwnerAddListing2ViewController: UIViewController {
    static var numberOfHotels:Int = 1


    let DataForCurrency : [String] = ["USD", "Rs"]
    let DataForDays : [String] = ["PerNight", "PerWeek", "PerMonth"]

    override func viewDidLoad() {
        super.viewDidLoad()

        currencyField.inputView = currencyPicker
        daysField.inputView = daysPicker
    }


    @IBAction func nextButtonTapped(_ sender: UIButton) {

        let currency = currencyField.text
        let charges = chargesField.text
        let days = daysField.text
        let phone = phoneField.text
        let email = emailField.text

        //Get reference to firebase Database
            let db = Firestore.firestore()

        //Post data tw database
        db.collection("Property").document("\(Auth.auth().currentUser!.uid)").collection("Hotel").document("\(OwnerAddListing2ViewController.numberOfHotels)").setData(["Currency": currency!, "Charges" : charges!, "Days" : days!, "Phone" : phone!, "EmailAddress" : email!], merge: true) {(error) in
                if error != nil {

                }
                else {
                    print("Data Posted Succesfully")
                    OwnerAddListing2ViewController.numberOfHotels = OwnerAddListing2ViewController.numberOfHotels + 1
                }

            }   
    }

1 个答案:

答案 0 :(得分:0)

静态变量仅将数据保存在一个应用程序生命周期中。重新启动应用程序后,静态变量将从默认值初始化。如果您要始终保留变量的值,也许应该考虑使用# print(df) A B C-1 D BTP Type C1 Type C2 3 0 4 3 3 42 [Type A, Type B] [Type A, Type B] ,它可以存储少量信息。但是请注意不要存储任何敏感数据,例如密码。