错误“表达式类型 '@lvalue 字符串"是什么意思?没有更多上下文是模棱两可的"吝啬的? [英] What does error "Expression type '@lvalue String?' is ambiguous without more context" mean?
本文介绍了错误“表达式类型 '@lvalue 字符串"是什么意思?没有更多上下文是模棱两可的"吝啬的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个 TableView 控制器中,我有多个 UITextFields 允许用户输入一些信息并使用 Realm 保存用户输入的数据.
In a TableView Controller, I have multiple UITextFields that allow users to input some information and use Realm to save data from users' input.
我使用以下方法添加数据,但收到错误消息表达式类型‘@lvalue 字符串?’没有更多上下文是模棱两可的"
I use the following ways to add the data but I got the error saying "Expression type '@lvalue String?' is ambiguous without more context"
import UIKit
import RealmSwift
class NewIdeaCreation: UITableViewController, UITextFieldDelegate {
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func createButtonPressed(_ sender: UIButton) {
try realm.write {
realm.add(nameTextField.text) //Error here
}
self.dismiss(animated: true, completion: nil)
}
@IBOutlet weak var nameTextField: UITextField!
}
我应该在这里做什么?
推荐答案
您只能将 Realm Object
保存到 Realm 数据库中.String
不是 Realm Object
.
You can only save Realm Object
s into a realm database. String
is not an Realm Object
.
您可以创建一个具有 String
的对象:
You can create an object that has a String
though:
class StringObject: Object {
@objc dynamic var string = ""
// Your model probably have more properties. If so, add them here as well
// If not, that's ok as well
}
现在你可以保存一个StringObject
:
do {
try realm.write {
let stringObject = StringObject()
stringObject.string = nameTextField.text ?? ""
realm.add(stringObject)
}
} catch let error {
print(error)
}
这篇关于错误“表达式类型 '@lvalue 字符串"是什么意思?没有更多上下文是模棱两可的"吝啬的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文