对于许多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", 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
的原因。如上面的代码所示,将特定的值分配给每个属性。
我是Swift的新手,但是在询问之前已经做了很多研究。对于我在做什么错的任何指示,我都将不胜感激。
TIA!
-----------------
编辑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
个对象,第二个检查旨在创建并保留一个新的Account
对象。
现在,testForActiveAccount
似乎可以正常工作-如果没有帐户,它将创建并命名一个帐户并将其持久保存到磁盘。然后以具有适当属性的命名帐户的形式打印出证明。
但是,使用基本相同代码的addAccountButton
似乎创建了一个空的Account
对象,该对象具有nil或空的属性。
我想,由于我对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)
}
这是每个功能的打印输出:
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
我知道我必须做些简单而愚蠢的事情-我只是不知道该做什么。有什么想法吗?
答案 0 :(得分:1)
这是您插入除参数之外的空实例的问题
let account = Account()
try! realm.write {
realm.add(account)
}
应该是
realm.add(thisAccount)
调用该方法时也不需要返回,因为您已经有了该实例,我也看到您在调用中忽略了它
答案 1 :(得分:0)
您应该尝试使用非静态扩展方法,它看起来更清晰,更易于使用:
func add(in realm: Realm = try! Realm()) {
try! realm.write {
realm.add(self)
}
}
答案 2 :(得分:0)
经过上述回答的大量研究后,我仍然遇到这里描述的问题。但是,我相信我已将罪魁祸首隔离为与该线程中怀疑的不正确的保存方法无关的东西-String literals
vs UITextField.text
,因为Realm属性的来源定义为{{1} } ...
因此,由于问题仍然存在,因此我在此new question中重申了该问题。
非常感谢您在这里的帮助!我仍然需要指导,因此,如果有兴趣,请查看新问题。