我是IOS的新手,试图实现一个数据库管理器类来检索行和保存。
获取错误"对NSEntityDescription,NSManagedObject,NSFetchRequest使用未解析的标识符。
我该如何解决这个问题?
class DatabaseManager: NSObject {
var pizzas: [NSManagedOject] = []
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func addRow( name:String, price:Int16) {
// set the core data to access the PizzaTable Entity
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "PizzaTable", in: managedContext)
let pizzas = NSManagedObject(entity: entity!, insertInto: managedContext)
pizzas.setValue(name, forKey: "name")
pizzas.setValue(price, forKey: "price")
do {
try managedContext.save()
pizzas.append(pizzas)
// showMessage("Information is added")
}
catch let error as NSError {
// showMessage("Error While Adding to Core Data")
}
}
func retrieveRows() -> [String] { // return array of Strings
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return [""]}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "PizzaTable")
do {
pizzas = try managedContext.fetch(fetchRequest)
}
catch let error as NSError{
// show something
}
var msg : [String] = []
var count = 0
for pizzas in pizzas {
msg.append((pizzas.value(forKeyPath: "name") as? String)!)
count += 1
}
// msg = String(count)
return msg
}
}