我因此定义了一个Realm模型:
// function to add a keep in touch note, as well as facebook and insta icons to emails
add_action( 'woocommerce_email_footer', 'update_footer_for_emails');
function update_footer_for_emails( $order )
{
$user_id = $order->object->get_billing_first_name();
$order_id = $order->object->id;
$email_greeting = sprintf( '<h1>Hi %s, please stay in touch</h1>', $user_id );
}
在View Controller中,我想使用两种方法实例化新的@objcMembers class Account: Object {
dynamic var name = ""
dynamic var currentBalance: Double = 0.00
dynamic var highWaterBalance: Double = 0.00
dynamic var isCurrent: Bool = false
}
对象:
1)
Account
2)
func testForActiveAccount(){
let accounts = realm.objects(Account.self)
let primaryAccount = Account()
if accounts.count < 1 {
primaryAccount.name = "Primary Account"
primaryAccount.isCurrent = true
primaryAccount.highWaterBalance = 0.00
addCreatedAccount(thisAccount: primaryAccount)
self.activeAcctLabel.text = primaryAccount.name
self.currentAccount = primaryAccount
}
}
这些函数中的每一个都调用此函数:
@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()
let nooAcctName = self.accountTextField.text!
newAccount.name = nooAcctName
newAccount.isCurrent = true
newAccount.highWaterBalance = 0.0
print("\nName of new account should read \(newAccount.name)\n")
self.addCreatedAccount(thisAccount: newAccount)
}
))
addAcctAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(addAcctAlert, animated: true, completion: nil)
}
func addCreatedAccount(thisAccount: Account){
print("\nPrinted from inside add function:\n")
try! realm.write {
realm.add(thisAccount)
let availableAccounts = realm.objects(Account.self)
print("There are now \(String(describing: availableAccounts.count)) Account objects \n")
for acct in availableAccounts{
print("acct.name is \(acct.name)")
print("acct.highWaterBalance is \(String(describing: acct.highWaterBalance))")
print("acct.isCurrent is \(String(describing: acct.isCurrent))")
}
}
}
正常工作,将新创建的addCreatedAccount
保存到磁盘。
问题出在这里
使用Account
中的字符串文字(Account.name
)创建的primaryAccount.name = "Primary Account"
属性可以使用,但是testForActiveAccount
中的newAccount.name = self.accountTextField.text!
所分配的字符串不会出现在addAccountButton
属性。
以下是从控制台读取的示例:
.name
肯定会感谢您的任何见解,提示,指示或任何其他帮助!
谢谢!