0
votes

I'm using MagicalRecord to create and query Core Data entities in a simple Swift app I'm creating. I have a basic view for creating an entity, using data from a text field. I'm getting the following error when I try to set an entity's name (let's call it Reference): Cannot assign to 'name' in 'referenceEntity'

Here's the save button that saves the entity:

@IBAction func saveBtn() {

        var referenceEntity = Reference.MR_createEntity()
        referenceEntity.name = nameTxt.text
    }

What am I doing wrong?

Thanks!

1

1 Answers

0
votes

Let me start by saying that Core Data is an incredibly powerful framework for persisting and maintaining object graphs

First step, you need an array to save the data for example:

var referenceEntities: [referenceEntity]!

Second step will be in your SaveBtn():

let referenceEntity = referenceEntity.MR_createEntity() as referenceEntity

Third step is to assign a value for referenceEntity.name:

referenceEntity.name = nameTxt.text

But wait, we have not stored our name in array, lets do it:

referenceEntities.append(referenceEntity)        

And finally we must save our data into DB and as you are using MagicalRecord the syntax would be:

NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreAndWait()

P.S this syntax works on MagicalRecord 2.3 and above

Hope this information will be helpful for someone.