Getting the error "unrecognized selector sent to instance" with core data and swift

6.8k views Asked by At

So, I have been working on this problem for a couple of days now. I have code that preloads data from a text file, parses it, and then converts it to an array of TestPreload Objects. These objects then go through my preload function and get stored into Test entities. This is my code for the Test.swift:

class Test: NSManagedObject {
    @NSManaged var id: Int;
    @NSManaged var name: String;
    @NSManaged var wTimeBreaks: Int;
    @NSManaged var wTimeSections: Int;
}

This is my code that parses my preload data:

let TestURL: NSURL;
let SectionURL: NSURL;
let Encoding: NSStringEncoding;
let Error: NSErrorPointer;
let delimiter = ",";

func parseTest()->[TestPreload] {
        var items:[TestPreload] = [];

        if let content = String(contentsOfURL: TestURL, encoding: Encoding, error: Error) {
            let lines: [String] = content.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String];

            for line in lines {
                var values: [String] = [];

                if(line != "") {
                    values = line.componentsSeparatedByString(delimiter);

                    let item = TestPreload(ID: values[0].toInt()!, Name: values[1], waitTimeBreaks: values[2].toInt()!, waitTimeSections: values[3].toInt()!);
                    items.append(item);
                }
            }
        }
        return items;
    }

This is my TestPreload.swift:

class TestPreload {
    var id: Int;
    var name: String;
    var wTimeBreaks: Int;
    var wTimeSections: Int;

    init(ID: Int, Name: String, waitTimeBreaks: Int, waitTimeSections: Int) {
        id = ID;
        name = Name;
        wTimeBreaks = waitTimeBreaks;
        wTimeSections = waitTimeSections;
    }
}

Finally, this is the preload function (where the data gets stored into CoreData), which is in my AppDelegate.swift file:

func preloadData() { let contentsOfTests = NSBundle.mainBundle().URLForResource("testPreload", withExtension: "csv"); let contentsOfSection = NSBundle.mainBundle().URLForResource("sectionPreload", withExtension: "csv");

    var error: NSError?;
    let Parser = CSVParse(tPreload: contentsOfTests!, sPreload: contentsOfSection!, encoding: NSUTF8StringEncoding, error: &error);
    let testData = Parser.parseTest();
    let sectionData = Parser.parseSection();

    if let managedObjectContext = self.managedObjectContext {
        for test in testData {
            let currentTest = NSEntityDescription.insertNewObjectForEntityForName("Test", inManagedObjectContext: managedObjectContext) as! Test;

            currentTest.id = test.id;
            currentTest.name = test.name;
            currentTest.wTimeBreaks = test.wTimeBreaks;
            currentTest.wTimeSections = test.wTimeSections;

            if(managedObjectContext.save(&error) != true) {
                println("insert error: \(error!.localizedDescription)");
            }
        }

        for section in sectionData {
            let currentSection = NSEntityDescription.insertNewObjectForEntityForName("Section", inManagedObjectContext: managedObjectContext) as! Section;

            currentSection.is_break = section.is_break;
            currentSection.is_editable = section.is_editable;
            currentSection.name = section.name;
            currentSection.sectionNumber = section.sectionNumber;
            currentSection.testID = section.testID;
            currentSection.time = section.time;

            if(managedObjectContext.save(&error) != true) {
                println("insert error: \(error!.localizedDescription)");
            }
        }
    }
}

I've looked in other places, and they all suggest that the problem is that my object isn't actually referencing a project. The weird part is that some of my statements work, such as currentSection.is_break = section.is_break;, however it throws and error when it reaches currentSection.sectionNumber = section.sectionNumber; This is the error that I get:

2015-06-11 13:03:16.631 satTimer[53733:3620372] -[satTimer.Section setSectionNumber:]: unrecognized selector sent to instance 0x7f9a81f543c0
2015-06-11 13:03:16.635 satTimer[53733:3620372] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[satTimer.Section setSectionNumber:]: unrecognized selector sent to instance 0x7f9a81f543c0'

I'm really ripping my hair out on this one, so any help would be great. Thanks in advance.

EDIT: These are my entities, Test and Section from my xcdatamodeld file: https://drive.google.com/file/d/0B-p9YZvcrWdHVmdkbHJldWFDUU0/view?usp=sharing

This is my code for Section.swift:

class Section: NSManagedObject {
    @NSManaged var is_break: Bool;
    @NSManaged var is_editable: Bool;
    @NSManaged var name: String;
    @NSManaged var sectionNumber: NSNumber;
    @NSManaged var testID: NSNumber;
    @NSManaged var time: NSNumber;
}

Once again, I can't thank you enough for just looking at this!

1

There are 1 answers

6
agupta231 On BEST ANSWER

Turns out there was an inconsistancy between my xcdatamodeld file and my objects. Thanks to everyone who spent time on this problem.

Cheers!