使用核心数据和swift获取错误"无法识别的选择器发送到实例"

agu*_*231 0 core-data selector ios swift

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

class Test: NSManagedObject {
    @NSManaged var id: Int;
    @NSManaged var name: String;
    @NSManaged var wTimeBreaks: Int;
    @NSManaged var wTimeSections: Int;
}
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
Run Code Online (Sandbox Code Playgroud)

这是我的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,这是preload函数(数据存储到CoreData中),它位于我的AppDelegate.swift文件中:

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)");
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我看过其他地方,他们都提出问题是我的对象实际上并没有引用一个项目.奇怪的部分是我的一些语句工作,例如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'
Run Code Online (Sandbox Code Playgroud)

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

编辑: 这是我的实体,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;
}
Run Code Online (Sandbox Code Playgroud)

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

agu*_*231 10

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

干杯!

  • 这是什么意思?什么是不一致的? (5认同)