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

Ios 控制台中打印的意外变量结果,ios,swift,string,realm,Ios,Swift,String,Realm,对于许多领域实体,我希望使用以下UIAlertController将每个领域实体的信息打印到控制台: @IBAction func addAccountButton(_ sender: Any) { // Pop alert to add new account let addAcctAlert = UIAlertController(title: "Create New Account", message: "Please name your new account", p

对于许多领域实体,我希望使用以下UIAlertController将每个领域实体的信息打印到控制台:

@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
(声明为
动态变量name=”“
)或者为什么
。highWaterBalance
(声明为
动态变量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
。第一个检查是否已经保存了
Account
对象,第二个用于创建和保存新的
Accountt
对象

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

但是,
addAccountButton
——它使用基本相同的代码——似乎创建了一个空的
Account
对象,其属性为零或空

我猜,在我对Swift和Realm都缺乏经验的情况下,我做出了错误的假设,或者遗漏了一个关键概念

以下是用于比较的全部功能:

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)

}

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

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

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

realm.add(thisAccount)

也不需要返回,因为调用此方法时您已经有了实例,而且我看到您在调用中忽略了它

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

func add(in realm: Realm = try! Realm()) {
    try! realm.write {
        realm.add(self)
    }
}

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

因此,由于问题仍然存在,我在本文中重申了这个问题


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

您也可以分享您的领域模型吗?帐户内有什么。添加(thisAccount请参见上面的编辑!请参见上面的新编辑!我正在尝试建议,但不确定我是否理解。您能否指定添加函数和UIAlertController调用的正确形式和位置?@Crattrap99为什么您首先给出
add(thisAccount:in:)
使用
thisAccount
变量,然后不按预期将其添加到域中,而是使用
Account()
创建一个空的Account对象,然后将该空对象插入域中?插入空的Account对象显然会产生空的变量条目,您可以很好地打印这些条目;)
func add(in realm: Realm = try! Realm()) {
    try! realm.write {
        realm.add(self)
    }
}