swift - 函数调用后对象发生变化

swift - object changes after function call

我有一个对象使用下面的代码

    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的结果给出了不同的值,

Optional("Hawn")

Optional("Toma")

所以它在 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("********************************************************************")
}

给予

Pet CD: Optional("Mel")

Pet CD: Optional("Joan")

如果我注释掉添加对象行,两个 NSLogs 都会给出正确的结果

Class 代码: setup - 只是设置数据库 导入 UIKit 导入核心数据

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
    }
}

宠物-核心数据属性class

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)
    }
    
}

以及进化核心数据class

import Foundation
import CoreData

extension Evolution {

    @NSManaged var evolveValue: String?
    @NSManaged var howEvolve: String?
    @NSManaged var evolveTo: Pet?

}

宠物class可以带多个进化对象,每个进化对象对应一只宠物

原因是双向关系。当您连接关系的一端时,另一端将为您连接,因此当您向集合(多端)添加内容时,另一端 属性(一端)将为您设置。这就是您所看到的。

如果这不是您想要的,那么您需要再添加 2 个关系,因为您确实希望所有关系都是双向的,但您还想将当前拥有的 2 个分开。