我是swift和核心数据的新手。我使用核心数据创建了一个简单的OS X应用程序。所以在AppDelegate.swift中有很多自动生成的代码。当我按下MainMenu.xib中的按钮时,我只想保存名称和姓氏。我一直在搜索如何在OS X应用程序中使用swift但没有成功的核心数据。 这是我的代码:
@IBAction func buttonAggiungiNewDipendentePressed(sender : AnyObject) {
println("Button pressed from popover");
//var AppDel:AppDelegate = (NSApplication.sharedApplication().delegate as AppDelegate)
let context = self.managedObjectContext!
var nome = popAggiungiDipendenteNome.stringValue;
var cognome = popAggiungiDipendenteCognome.stringValue;
var newUser = NSEntityDescription.insertNewObjectForEntityForName("Dipendenti", inManagedObjectContext: context) as NSManagedObject
newUser.setValue(nome, forKey: "nome")
newUser.setValue(cognome, forKey: "cognome")
context.save(nil)
popover.performClose(true);
}
我收到了以下崩溃:线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)在AppDelegate.swift中的自动生成代码中:
var managedObjectModel: NSManagedObjectModel? {
// Creates if necessary and returns the managed object model for the application.
if let mom = _managedObjectModel {
return mom
}
let modelURL = NSBundle.mainBundle().URLForResource("ElencoTelefonicoAkhela2", withExtension: "momd")
_managedObjectModel = NSManagedObjectModel(contentsOfURL: modelURL)
return _managedObjectModel!
}
var _managedObjectModel: NSManagedObjectModel? = nil
尝试返回managedObjectModel时。任何的想法? Thanx提前
答案 0 :(得分:1)
而不是insertNewObjectForEntityForName使用:
let en = NSEntityDescription.entityForName("Photo", inManagedObjectContext: context)
let photo:Photo = Photo(entity:en, insertIntoManagedObjectContext:context)