![](/img/trans.png)
[英]Click button getting error : unrecognized selector sent to instance - IOS Swift 3
[英]Getting the error “unrecognized selector sent to instance” with core data and swift
因此,我已經在這個問題上工作了幾天。 我的代碼可以從文本文件中預加載數據,進行解析,然后將其轉換為TestPreload
Objects數組。 這些對象然后通過我的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 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;
時會拋出錯誤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
和我的xcdatamodeld文件中的Section
: 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
文件與對象之間存在不一致。 感謝所有在此問題上花費時間的人。
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.