使用核心数据和 swift 获取错误 "unrecognized selector sent to instance"
Getting the error "unrecognized selector sent to instance" with core data and 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;
}
这是我解析预加载数据的代码:
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() {
让 contentsOfTests = NSBundle.mainBundle().URLForResource("testPreload", withExtension: "csv");
让 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'
我真的在这件事上扯了我的头发,所以任何帮助都会很棒。提前致谢。
编辑:
这些是我的实体,Test
和 Section
来自我的 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
文件和我的对象之间存在不一致。感谢所有花时间解决这个问题的人。
干杯!
所以,我这几天一直在研究这个问题。我的代码可以从文本文件预加载数据,对其进行解析,然后将其转换为 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() { 让 contentsOfTests = NSBundle.mainBundle().URLForResource("testPreload", withExtension: "csv"); 让 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'
我真的在这件事上扯了我的头发,所以任何帮助都会很棒。提前致谢。
编辑:
这些是我的实体,Test
和 Section
来自我的 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
文件和我的对象之间存在不一致。感谢所有花时间解决这个问题的人。
干杯!