问题描述
我想知道如何基本上为我的CoreData NSManagedObject客户端创建相当于一个类别:
我知道扩展现在是Swift中类别的替换 - 但是我不确定它们的实现。这是我在下面扣除的:
class Client:NSManagedObject {
@NSManaged var name: String
@NSManaged var projects:NSSet
}
扩展客户端{
func addClientWithName(name:NSString,context:NSManagedObjectContext) - >客户端{
var client:Client = Client();
//检查数据库中的名称是否已经存在
let request:NSFetchRequest = NSFetchRequest(entityName:Client)
let sortDescriptor:NSSortDescriptor = NSSortDescriptor ,ascending:true)
let sortDescriptorsAry:NSArray = [sortDescriptor]
request.sortDescriptors = sortDescriptorsAry
request.predicate = NSPredicate(format:name =%@,name)
var error:NSError?
let matches:NSArray = context.executeFetchRequest(request,error:& error)
如果matches.count == 0 {
//添加新客户
var newClient:NSEntityDescription = NSEntityDescription.insertNewObjectForEntityForName(Client,inManagedObjectContext:context)as NSEntityDescription
newClient.setValue(name,forKey:name)
context.save (零);
} else {
client = matches.lastObject as Client
}
返回客户端
}
}
但是,这是我到目前为止的错误:
CoreData:错误:无法调用NSManagedObject类Ttc9AppName6Client上的指定初始化程序
有什么可能导致这种情况的想法?
编辑:
这是我从其他类调用方法的方法:
@IBAction func saveBtnTapped(UIButton){
println(save btn tapped)
var client:Client = Client()。addClientWithName Woofy Face,context:self.managedObjectContext)
}
解决方案我认为你的问题与类别/扩展无关。
var client:Client = Client
不会调用 NSManagedObject的指定初始化程序
,如果获取请求没有找到匹配的对象,则返回此对象
。
你可能想要的是返回新的
请注意
insertNewObjectForEntityForName: inManagedObjectContext:
返回受管对象,而不是实体描述。
此外,
var client:Client = Client()。addClientWithName $ b
还创建一个Client对象,而不使用指定的初始化程序。
您应该将方法定义为类方法。
因此,您的代码应如下所示:
class func addClientWithName(name:NSString,context:NSManagedObjectContext) - >客户端{
var client:Client
让请求:NSFetchRequest = NSFetchRequest(entityName:Client)
let sortDescriptor:NSSortDescriptor = NSSortDescriptor(key:name,ascending: true)
let sortDescriptorsAry:NSArray = [sortDescriptor]
request.sortDescriptors = sortDescriptorsAry
request.predicate = NSPredicate(format:name =%@,name)
var error:NSError?
let matches:NSArray = context.executeFetchRequest(request,error:& error)
如果matches.count == 0 {
//添加新客户端
client = NSEntityDescription.insertNewObjectForEntityForName(Client,inManagedObjectContext:context)as Client
client.name = name
context.save(nil)
} else {
client = matches。 lastObject as Client
}
return client
}
将被称为
let client:Client = Client.addClientWithName(Woofy Face,context:self.managedObjectContext)
I'm wondering how to essentially create the equivalent of a Category for my CoreData NSManagedObject Client below:
I understand that Extensions are now the replacement of Categories in Swift - however am unsure of their implementation. Here is what I have deducted below:
class Client: NSManagedObject {
@NSManaged var name: String
@NSManaged var projects: NSSet
}
extension Client {
func addClientWithName(name:NSString, context:NSManagedObjectContext) -> Client {
var client:Client = Client();
// Check for name in the database already exists
let request:NSFetchRequest = NSFetchRequest(entityName: "Client")
let sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let sortDescriptorsAry:NSArray = [sortDescriptor]
request.sortDescriptors = sortDescriptorsAry
request.predicate = NSPredicate(format: "name = %@", name)
var error:NSError?
let matches:NSArray = context.executeFetchRequest(request, error: &error)
if matches.count == 0 {
//Add new Client
var newClient:NSEntityDescription = NSEntityDescription.insertNewObjectForEntityForName("Client", inManagedObjectContext: context) as NSEntityDescription
newClient.setValue(name, forKey: "name")
context.save(nil);
} else {
client = matches.lastObject as Client
}
return client
}
}
With this implementation however, this is the error i get so far:
CoreData: error: Failed to call designated initializer on NSManagedObject class "Ttc9AppName6Client"
Any ideas on what could be causing this?
Edit:This is how i call the method from my other class:
@IBAction func saveBtnTapped(UIButton) {
println("save btn tapped")
var client:Client = Client().addClientWithName("Woofy Face", context: self.managedObjectContext)
}
解决方案 I think your problem is unrelated to categories/extensions.
var client:Client = Client();
does not call the designated initializer of NSManagedObject
, and this objectis returned if the fetch request found no matching object.
What you probably want is to return the newly created object in that case.
Note also that
insertNewObjectForEntityForName:inManagedObjectContext:
returns a managed object, not an entity description.
In addition,
var client:Client = Client().addClientWithName(...)
also creates a Client object without using the designated initializer.You should define the method as a class method instead.
So your code should look like this:
class func addClientWithName(name:NSString, context:NSManagedObjectContext) -> Client {
var client:Client
let request:NSFetchRequest = NSFetchRequest(entityName: "Client")
let sortDescriptor:NSSortDescriptor = NSSortDescriptor(key: "name", ascending: true)
let sortDescriptorsAry:NSArray = [sortDescriptor]
request.sortDescriptors = sortDescriptorsAry
request.predicate = NSPredicate(format: "name = %@", name)
var error:NSError?
let matches:NSArray = context.executeFetchRequest(request, error: &error)
if matches.count == 0 {
//Add new Client
client = NSEntityDescription.insertNewObjectForEntityForName("Client", inManagedObjectContext: context) as Client
client.name = name
context.save(nil)
} else {
client = matches.lastObject as Client
}
return client
}
and it would be called as
let client:Client = Client.addClientWithName("Woofy Face", context: self.managedObjectContext)
这篇关于CoreData - 使用类别w /示例 - Swift的等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!