How to insert new data to entity in Swift3?

前端 未结 4 1556
佛祖请我去吃肉
佛祖请我去吃肉 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>=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.

    0 讨论(0)
  • 2021-02-20 15:17

    I'm not an core-data expert, but the API offers 3 distinct ways of creating an NSManagedObject instance. I'm not aware if any of them offer any benefit over the other.

    Assuming you have an entity/NSManagedObject subclass called TestEntity then all of the below are the same:

    New way (no casting required) +iOS10:

    let test = TestEntity(context: context)
    test.attribute1 = "a value"
    

    Older ways (casting is not required if you're ok with using setValue. With casting you can use dot notation. +iOS3

    let testEntity =  NSEntityDescription.entity(forEntityName: "TestEntity", in: context)
    
    let test1 = NSManagedObject(entity: testEntity!, insertInto: context)
    test1.setValue("a value", forKey: "attribute1")
    
    let test2 = NSManagedObject(entity: testEntity!, insertInto: context) as! TestEntity
    test2.attribute1 = "a value"
    

    or:

    let test1 = NSEntityDescription.insertNewObject(forEntityName: "TestEntity", into: context)
    test1.setValue("a value", forKey: "attribute1") 
    
    let test2 = NSEntityDescription.insertNewObject(forEntityName: "TestEntity", into: context) as! TestEntity
    test2.attribute1 = "a value"
    

    once you create the NSManagedObject instance and assign its attributes then you just save it in the context.

    do {
        try context.save()
        print("saved!")
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    } catch {
    
    }
    
    0 讨论(0)
  • 2021-02-20 15:23

    If there is a NSManagedObject subclass TestEntity the new syntax is

    let entity = TestEntity(context: context)
    entity.testAttribute="test value"
    
    0 讨论(0)
  • 2021-02-20 15:34
    //retrieve the entity
    let entity =  NSEntityDescription.entity(forEntityName: "TestEntity", in: context)
    
    let testEntity = NSManagedObject(entity: entity!, insertInto: context)
    
    //set the entity values
    testEntity.setValue(testAtt1, forKey: "testAtt1")
    testEntity.setValue(testAtt2, forKey: "testAtt2")
    
    //save the object
    do {
        try context.save()
        print("saved!")
    } catch let error as NSError  {
        print("Could not save \(error), \(error.userInfo)")
    } catch {
    
    }
    
    0 讨论(0)
提交回复
热议问题