代码之家  ›  专栏  ›  技术社区  ›  rattletrap99

控制台中打印的意外变量结果

  •  0
  • rattletrap99  · 技术社区  · 7 年前

    @IBAction func addAccountButton(_ sender: Any) {
    
        // Pop alert to add new account
    
        let addAcctAlert = UIAlertController(title: "Create New Account", message: "Please name your new account", preferredStyle: UIAlertControllerStyle.alert)
    
        addAcctAlert.addTextField{ (accountTextField) in
            accountTextField.placeholder = "Enter new account name"
            accountTextField.keyboardType = .`default`
        }
    
        addAcctAlert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.destructive, handler: { action in
    
            // Create new Account with name, balance, isCurrent, save to disk
    
            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
            newAccount.isCurrent = false
            newAccount.highWaterBalance = 0.0
    
            // Adds the newly created account
    
            Account.add(thisAccount: newAccount)
    
            let realm = try! Realm()
            let currentAccounts = realm.objects(Account.self)
    
            print ("In AddAccountButton func:")
            print("There are \(String(describing: currentAccounts.count)) Account objects")
    
            for acct in currentAccounts{
                print("acct.name is \(acct.name)")
                print("acct.highWaterBalance is \(String(describing: acct.highWaterBalance))")
                print("acct.isCurrent is \(String(describing: acct.isCurrent))")
    
            }
        }))
    
        addAcctAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
    
        self.present(addAcctAlert, animated: true, completion: nil)
    
    }
    

    下面是我得到的结果,尽管在创建新对象时每个属性都被赋值:

    There are 4 Account objects
    acct.name is 
    acct.highWaterBalance is nil
    acct.isCurrent is false
    acct.name is 
    acct.highWaterBalance is nil
    acct.isCurrent is false
    acct.name is 
    acct.highWaterBalance is nil
    acct.isCurrent is false
    acct.name is 
    acct.highWaterBalance is nil
    acct.isCurrent is false
    

    按预期打印的唯一属性是 acct.isCurrent .name (声明为 dynamic var name = "" )或者为什么 .highWaterBalance dynamic var highWaterBalance: Float? )正在打印为 nil

    我是斯威夫特的新手,但在问之前我已经研究了很多。如果你能指点我做错了什么,我一定会很感激的。

    -----------------

    编辑1:

    导入基础 导入RealmSwift

    @objcMembers类帐户:对象{

    dynamic var name = ""
    dynamic var currentBalance: Float?
    dynamic var highWaterBalance: Float?
    dynamic var isCurrent: Bool = false
    

    扩展帐户{

    // Adds an item in realm, a new transaction object
    
    @discardableResult
    static func add(thisAccount: Account, in realm: Realm = try! Realm())
        -> Account {
            let account = Account()
            try! realm.write {
                realm.add(account)
            }
            return account
    }
    

    -----------------

    编辑2:

    我的代码,我从 Account 类定义。现在我使用以下两个函数, testForActiveAccount addAccountButton . 第一次检查是否已经有 对象已保存,而第二个对象旨在创建并保存新的 帐户

    现在, 似乎工作正常——如果没有帐户,它会创建并命名一个帐户并将其持久化到磁盘。然后,它以具有适当属性的命名帐户的形式打印出证据。

    然而, --它使用基本相同的代码——似乎创建了一个空的 帐户 对象,具有零或空属性。

    下面是用于比较的整个函数:

    func testForActiveAccount(){
    
        let realm = try! Realm()
        let accounts = realm.objects(Account.self)
        let primaryAccount = Account()
    
        if accounts.count < 1 {
            primaryAccount.name = "Primary Account"
            primaryAccount.isCurrent = true
            primaryAccount.highWaterBalance = 0.00
    
            try! realm.write {
                realm.add(primaryAccount)
            }
            self.activeAcctLabel.text = primaryAccount.name
            self.currentAccount = primaryAccount
    
            for acct in accounts{
    
                print ("In testForActiveAccount func:\n")
                print("There are \(String(describing: accounts.count)) Account objects \n")
    
                print("acct.name is \(acct.name)")
                print("acct.highWaterBalance is \(String(describing: acct.highWaterBalance))")
                print("acct.isCurrent is \(String(describing: acct.isCurrent))")
    
            }
        }
    }
    
    
    @IBAction func addAccountButton(_ sender: Any) {
    
        // Pop alert to add new account
    
        let addAcctAlert = UIAlertController(title: "Create New Account", message: "Please name your new account", preferredStyle: UIAlertControllerStyle.alert)
    
        addAcctAlert.addTextField{ (accountTextField) in
            accountTextField.placeholder = "Enter new account name"
            accountTextField.keyboardType = .`default`
        }
    
        addAcctAlert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.destructive, handler: { action in
    
            // Create new Account with name, balance, isCurrent, save to disk
    
            let realm = try! Realm()
            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
            newAccount.isCurrent = false
            newAccount.highWaterBalance = 0.0
    
            try! realm.write {
                realm.add(newAccount)
            }
    
            let currentAccounts = realm.objects(Account.self)
    
            print ("In AddAccountButton func:")
            print("There are \(String(describing: currentAccounts.count)) Account objects \n")
    
            for acct in currentAccounts{
                print("acct.name is \(acct.name)")
                print("acct.highWaterBalance is \(String(describing: acct.highWaterBalance))")
                    print("acct.isCurrent is \(String(describing: acct.isCurrent))\n")
    
            }
    
        }))
    
        addAcctAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
    
        self.present(addAcctAlert, animated: true, completion: nil)
    
    }
    

    In testForActiveAccount func:
    
    There are 1 Account objects 
    
    acct.name is Primary Account
    acct.highWaterBalance is nil
    acct.isCurrent is true
    
    In AddAccountButton func:
    There are 2 Account objects 
    
    acct.name is Primary Account
    acct.highWaterBalance is nil
    acct.isCurrent is true
    
    acct.name is 
    acct.highWaterBalance is nil
    acct.isCurrent is false
    

    我知道我一定是在做一些简单而愚蠢的事情——我只是不知道是什么。有什么想法吗?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Shehata Gamal    7 年前

    这里是您插入空实例而不是参数的问题

    let account = Account()
    try! realm.write {
         realm.add(account)
    }
    

    应该是

    realm.add(thisAccount)
    

        2
  •  0
  •   Peter Nagy    7 年前

    您应该尝试使用非静态扩展方法,它看起来更清晰、更易于使用:

    func add(in realm: Realm = try! Realm()) {
        try! realm.write {
            realm.add(self)
        }
    }
    
        3
  •  0
  •   rattletrap99    7 年前

    在以上答案的指导下进行了大量的调查之后,我仍然有这里描述的问题。不过,我相信我已经将罪魁祸首与此线程中怀疑的不正确的保存方法无关-- String literals UITextField.text String ...

    因此,由于问题仍然存在,我在这里重申了这个问题 new question

    非常感谢您的帮助!我仍然需要指导,所以如果有兴趣,请查看新问题。