0
votes

I am trying to save in Int to core data and am struggling.

I have an array with a SQLite with about 2000 records and all are listed on a tableview. When one of the records are selected, it goes to the "speciesDetailViewController" were it displays details of that item.

all currently displayed is not editable.

I am adding the ability for the user to add notes and the option to change one of the fields.

The minor changes are saved in CoreData as I have no experience with SQLite (hired someone).

I have all working, except saving the Int.

Below are other details.

var specieId: Int = -1
var speciesDetailData : SpeciesDetailData?
lazy var coreDataStack = CoreDataStack()

The below is the save func used.

func saveResults () {


    if let speciesDetailData = speciesDetailData {

        let x = Int32(self.specieId)

        speciesDetailData.commonName = ckCommonNameTextField.text!
        speciesDetailData.commonForeignName = ckCommonForeignNameTextField.text!.isEmpty ? "" : ckCommonForeignNameTextField.text
        speciesDetailData.speciesName = speciesLabel.text!
        speciesDetailData.speciesID = x
        speciesDetailData.speciesNote = ckNotesTextView.text!.isEmpty ? "" : ckNotesTextView.text

    } else if speciesDetailData == nil {

        if let savedData = NSEntityDescription.entity(forEntityName: "SpeciesDetailData", in: coreDataStack.managedObjectContext1) {

            let x = Int32(self.specieId)

            let speciesDetailData = SpeciesDetailData(entity: savedData, insertInto: coreDataStack.managedObjectContext1)

            print (specieId)

            speciesDetailData.commonName = ckCommonNameTextField.text!.isEmpty ? "" : ckCommonNameTextField.text
            speciesDetailData.commonForeignName = ckCommonForeignNameTextField.text!.isEmpty ? "" : ckCommonForeignNameTextField.text
            speciesDetailData.speciesName = speciesLabel.text!
            speciesDetailData.speciesID = x
            speciesDetailData.speciesNote = ckNotesTextView.text!.isEmpty ? "" : ckNotesTextView.text

        }
    }

    coreDataStack.saveMainContext()

The entity is SpeciesDetailData and the speciesID is currently set to Int32.

I have tried the above, I have also tried

speciesDetailData.speciesID = Int32(self.specieId)
speciesDetailData.speciesID = Int32(specieId)

I have tried to set all to Int64, same results.

I have seen other posts, but all suggestions are the same as what I have tried.

Also, I am not getting an error. I only get the red line of death.

I set a breakpoint and followed it through and when it hits the int line, it just goes red lined with no details.

UPDATE: I have no idea what "User Scalar Type" is, but I unchecked it and the autocorrect changed it to the below now runs without an error but a nil is saved. I ran the print (specieId) and it prints the correct integer.

 speciesDetailData.speciesID = Int32(specieId) as NSNumber?

Any suggestions?

1

1 Answers

0
votes

Apparently, use Scalar type is to assist with the migration to Swift 3. When I unchecked this box, it seems to run OK. I ended up needing to add as NSNumber at the backend of the line, but it works.