ios - 使用核心数据和swift

ios - 使用核心数据和swift,第1张

所以,我已经在这个问题上工作了几天了。我有代码从文本文件预加载数据,解析它,然后将其转换为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;
    }
}

最后,这是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)");
            }
        }
    }
}

我看过其他地方,他们都提出问题是我的对象实际上并没有引用一个项目。奇怪的是,我的一些语句可以工作,例如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'

我真的把这头发撕掉了,所以任何帮助都会很棒。提前谢谢。

修改 这些是我的xcdatamodeld文件中的实体TestSectionhttps://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;
}

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

最佳答案:

1 个答案:

答案 0 :(得分:9)

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

干杯!

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复