NSSet和核心数据

时间:2015-06-02 04:06:32

标签: swift

我正在尝试在核心数据中保存两个对象列表。我有一个协议方法,将我的标签添加为(Tag)并将它们放入一个标签数组中。我无法保存这些对象,因为我正在使用NSSet和扩展名(对于swift):

extension CurrentTransaction {
func addTagObject(value:Tag) {
    var items = self.mutableSetValueForKey("tagMembers");
    items.addObject(value)
 }

 func removeTagObject(value:Tag) {
    var items = self.mutableSetValueForKey("tagMembers");
    items.removeObject(value)
 }
}

我试图保存的核心数据中的两个对象是:

class Pic: NSManagedObject {

@NSManaged var picName: String!
@NSManaged var picNumber: String!
@NSManaged var currentTransaction: CurrentTransaction

class func createInManagedObjectContext(moc: NSManagedObjectContext, name: String, number: String) -> Pic {
    let newPic = NSEntityDescription.insertNewObjectForEntityForName("Pic", inManagedObjectContext: moc) as! Pic
    newPic.picName = name
    newPic.picNumber = number

    return newPic
}
}

类标记:

class Tag: NSManagedObject {

@NSManaged var tagName: String!
@NSManaged var tagNumber: String!
@NSManaged var currentTransaction: CurrentTransaction

class func createInManagedObjectContext(moc: NSManagedObjectContext, name: String, number: String) -> Tag {
    let newTag = NSEntityDescription.insertNewObjectForEntityForName("Tag", inManagedObjectContext: moc) as! Tag
    newTag.tagName = name
    newTag.tagNumber = number

    return newTag
}


}

class currentTransaction:

class CurrentTransaction: NSManagedObject {

@NSManaged var destinationPIC: String
@NSManaged var sourcePIC: String
@NSManaged var nvdNumber: String
@NSManaged var numberOfAnimals: NSNumber
@NSManaged var requestType: String
@NSManaged var date: NSDate
@NSManaged var tagMembers: NSSet
@NSManaged var picMembers: NSSet?


class func createInManagedObjectContext(moc: NSManagedObjectContext, toPic: String, fromPic: String, nvd: String, numAnimals: NSNumber, request: String, date: NSDate, tags: NSSet, pics: NSSet) -> CurrentTransaction {

    let newTransaction = NSEntityDescription.insertNewObjectForEntityForName("CurrentTransaction", inManagedObjectContext: moc) as! CurrentTransaction

    newTransaction.destinationPIC = toPic
    newTransaction.sourcePIC = fromPic
    newTransaction.nvdNumber = nvd
    newTransaction.numberOfAnimals = numAnimals
    newTransaction.requestType = request
    newTransaction.tagMembers = tags
    newTransaction.picMembers = pics
    newTransaction.date = date

    return newTransaction
}

}

当我调用CurrentTransaction.addTagObject(tag:Tag)时,我得到“无法使用类型'(标签)'的参数列表调用'addTagObject'

这是我的addTags协议方法:

func addTags(tag: Tag) {
    //println("IN ADD TAGS")
    //println(tag.tagNumber)
    self.tagList.append(tag)
    CurrentTransaction.addTagObject(tag) //Error on this line
}

1 个答案:

答案 0 :(得分:0)

原因:addTagObject()未定义为类函数,无法在类CurrentTransaction上调用。如果您打算将其用作类功能,请将其更改为:

class func addTagObject(value:Tag) {
    var items = self.mutableSetValueForKey("tagMembers");
    items.addObject(value)
}