【发布时间】:2018-01-11 19:31:02
【问题描述】:
我像这样将文本视图中的值保存到核心数据
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newAttr = NSEntityDescription.insertNewObject(forEntityName: "TableName", into: context )
let attribute_name = textView.text
newAttr.setValue(attribute_name, forKey "attr_name")
context.save()
然后我尝试像这样读回它:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest: NSFetchRequest = TableName.fetchRequest()
let cdRowsArr = try context.fetch(fetchRequest)
let cdRow = cdRows[0]
let txtViewDesc = String(describing: cdRow.value(forKey: "attr_name"))
let attrs = [NSFontAttributeName : UIFont.boldSystemFont(ofSize:15)]
let boldString = NSMutableAttributedStrign(string: txtViewDesc, attributes: attrs)
textView.attributedText = boldString
而且由于某种原因,每次输出都是 Optional("originalTextValue") 或 nil,如果它是空的
我不明白为什么我想要的值被可选
【问题讨论】: