使用核心数据和快速获取错误"unrecognized selector sent to instance"



所以,我已经研究这个问题几天了。我有代码从文本文件预加载数据,解析它,然后将其转换为TestPreload对象数组。然后,这些对象通过我的preload函数并存储到Test实体中。这是我Test.swift的代码:

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

这是我解析预加载数据的代码:

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;
    }

这是我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;
    }
}

最后,这是预加载函数(数据存储到 CoreData 中),它在我的AppDelegate.swift文件中:

func preloadData() { let contentOfTests = NSBundle.mainBundle()。URLForResource("testPreload", withExtension: "csv"); let contentOfSection = 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)");
            }
        }
    }
}

我看过其他地方,它们都表明问题在于我的对象实际上并没有引用项目。奇怪的部分是我的一些语句有效,例如 currentSection.is_break = section.is_break; ,但是当它到达currentSection.sectionNumber = section.sectionNumber;时它会抛出并出错 这是我得到的错误:

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'

我真的在这个问题上扯掉了我的头发,所以任何帮助都会很棒。提前谢谢。

编辑:这些是我的实体,TestSection我的xcdatamodeld文件:https://drive.google.com/file/d/0B-p9YZvcrWdHVmdkbHJldWFDUU0/view?usp=sharing

这是我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;
}

再一次,我不能感谢你只是看着这个!

事实证明,

我的xcdatamodeld文件和我的对象之间存在不一致。感谢所有花时间解决这个问题的人。

干杯!

相关内容

最新更新