How to insert new data to entity in Swift3?

前端 未结 4 1461
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 14:56

In swift previously, I was able to use a code like this to add new data to my \"TestEntity\" in my data model.

NSManagedObject was created for my \"TestEnti

4条回答
  •  梦谈多话
    2021-02-20 15:15

    working example:

        let fr:NSFetchRequest=TestEntity.fetchRequest()
        do {
            let results=try self.moc!.fetch(fr)
            if results.count==0 {
                print("no resutls. i need to add something")
                let newTestEntity=TestEntity(context: self.moc!)
                newTestEntity.testAtt="testovaci text"
                do {
                    try self.moc!.save()
                }catch{
    
                }
            }else{
                print("already some results")
                for result in results {
                    print(result.testAtt!)
                }
            }
        }catch {
            print(error)
        }
    

    Data model inspector TestEntity Class name needs to be set to TestEntity. Previous strange behaviour seemed to be caused by this value to be blank.

提交回复
热议问题