我有一个使用以下代码的对象
name = "Toma"
let toma = setupPet(name, family: "Hawn", stage: 1, imageName: "hawn_toma", imageNamef: "", imageNameEgg: "hawn_egg", hatchEXP: 5000, desc: "", eggDesc: "", rarity: "Common")
name = "Hawn"
let hawn = setupPet(name, family: "Hawn", stage: 2, imageName: "hawn", imageNamef: "", imageNameEgg: "hawn_egg", hatchEXP: 5000, desc: "", eggDesc: "", rarity: "Common")
hawn.basePet = toma
let tomaToHawn = NSEntityDescription.insertNewObjectForEntityForName("Evolution", inManagedObjectContext: self.appData.moc) as! Evolution
tomaToHawn.evolveTo = hawn
tomaToHawn.howEvolve = "Level"
tomaToHawn.evolveValue = "10"
NSLog("\(tomaToHawn.evolveTo!.name)")
toma.addEvolveToObject(tomaToHawn)
NSLog("\(tomaToHawn.evolveTo!.name)")
现在两个NSLog的结果给出了不同的值,
可选( “霍恩”)
可选( “托马”)
所以在addEvolveToObject调用之后它发生了变化 该方法包括
class Pet: NSManagedObject {
func addEvolveToObject(value:Evolution) {
let items = self.mutableSetValueForKey("evolveTo");
items.addObject(value)
}
}
我也尝试使用@NSManaged func添加进化,但得到相同的结果。将对象添加到nsset后,evolveto将被设置为要添加到的宠物。其他字段保留正确的数据
编辑 我已将问题缩小到addEvolveToObject方法。如果我在items.addobject调用之前使用NSLog(“Pet CD:(value.evolveTo?.name)”)打印它会给出正确的值,但如果我在语句后打印相同的值,则会给出不同的值
IE:
func addEvolveToObject(value:Evolution) {
NSLog("********************************************************************")
let items = self.mutableSetValueForKey("evolveTo");
NSLog("Pet CD: \(value.evolveTo?.name)")
items.addObject(value)
NSLog("Pet CD: \(value.evolveTo?.name)")
NSLog("********************************************************************")
}
给出
宠物CD:可选(“Mel”)
宠物CD:可选(“Joan”)
如果我注释掉添加对象行,两个NSLog都会给出正确的结果
班级代码: 设置 - 只需设置数据库 导入UIKit 导入CoreData
class Setup: NSObject {
let appData : AppData = AppData.sharedInstance
func setup() {
// Hawn
name = "Toma"
let toma = setupPet(name, family: "Hawn", stage: 1, imageName: "hawn_toma", imageNamef: "", imageNameEgg: "hawn_egg", hatchEXP: 5000, desc: "", eggDesc: "", rarity: "Common")
name = "Hawn"
let hawn = setupPet(name, family: "Hawn", stage: 2, imageName: "hawn", imageNamef: "", imageNameEgg: "hawn_egg", hatchEXP: 5000, desc: "", eggDesc: "", rarity: "Common")
hawn.basePet = toma
let tomaToHawn = NSEntityDescription.insertNewObjectForEntityForName("Evolution", inManagedObjectContext: self.appData.moc) as! Evolution
tomaToHawn.evolveTo = hawn
tomaToHawn.howEvolve = "Level"
tomaToHawn.evolveValue = "10"
toma.addEvolveToObject(tomaToHawn)
appData.saveContext()
}
func setupPet(name : String, family : String, stage : NSNumber, imageName : String, imageNamef : String, imageNameEgg : String, hatchEXP : NSNumber, desc : String, eggDesc : String, rarity : String) -> Pet {
let pet : Pet = NSEntityDescription.insertNewObjectForEntityForName("Pet",inManagedObjectContext: appData.moc) as! Pet
pet.name = name
pet.family = family
pet.stage = stage
pet.imageName = imageName
pet.imageNamef = imageNamef
pet.imageNameEgg = imageNameEgg
pet.hatchEXP = hatchEXP
pet.rarity = rarity
pet.desc = desc
pet.eggDesc = eggDesc
return pet
}
}
Pet - 核心数据属性类
import Foundation
import CoreData
extension Pet {
@NSManaged var desc: String?
@NSManaged var eggDesc: String?
@NSManaged var family: String?
@NSManaged var gained: NSNumber?
@NSManaged var hatchEXP: NSNumber?
@NSManaged var imageName: String?
@NSManaged var imageNameEgg: String?
@NSManaged var imageNamef: String?
@NSManaged var name: String?
@NSManaged var rarity: String?
@NSManaged var stage: NSNumber?
@NSManaged var adopt: NSSet?
@NSManaged var evolveTo: NSSet?
@NSManaged var userPets: NSSet?
@NSManaged var basePet: Pet?
}
宠物
import Foundation
import CoreData
class Pet: NSManagedObject {
func addEvolveToObject(value:Evolution) {
let items = mutableSetValueForKey("evolveTo") as NSMutableSet;
items.addObject(value)
}
}
进化核心数据类
import Foundation
import CoreData
extension Evolution {
@NSManaged var evolveValue: String?
@NSManaged var howEvolve: String?
@NSManaged var evolveTo: Pet?
}
宠物类可以携带多个进化对象,每个进化对象用于一只宠物
答案 0 :(得分:2)
原因是双向关系。当您连接关系的一端时,另一端将为您连接,因此当您向集合添加内容(多端)时,将为您设置另一个属性(一端)。这就是你所看到的。
如果这不是您想要的,那么您需要再添加2个关系,因为您确实希望所有关系都是双向的,但您也希望将您当前拥有的关系分开。