我如何检查我的核心数据库中的字段,如果我想添加一个实体,怎么可能?下面是我保存数据的代码,我想使用键杂货项目将除名称之外的其他数据添加到我的核心数据库表中。
func saveItemInLocalDB(groceryItem : Item) {
let context = getContext()
//retrieve the entity that we just created
let entity = NSEntityDescription.entity(forEntityName: "GroceryItem", in: context)
let item = NSManagedObject(entity: entity!, insertInto: context)
//set the entity values
item.setValue(groceryItem.name, forKey: "name")
item.setValue(false, forKey: "isSelected")
item.setValue(loggedInUserHouseNumber, forKey: "houseNo")
//save the object
do {
try context.save()
print("ang item:", groceryItem.name)
print("saved!")
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
} catch {
}
}
你可以试试这个
let IsFeildAvailable = entity.attributesByName.keys.contains("keyname")
if IsFeildAvailable
{
print("Key/field is exist")
}else{
print("Key/field does not exist")
}
关于ios - swift ios如何检查核心数据库中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49570072/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |