Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

swift - object changes after function call

desbrinadesbrina HertfordshirePosts: 55Registered Users @
I have several objects created using the below code
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)")

Now the result of the two NSLogs gives different values
Optional("Hawn")
Optional("Toma")

So it changed after the addEvolveToObject call. The method consists of
class Pet: NSManagedObject {

    func addEvolveToObject(value:Evolution) {
        let items = self.mutableSetValueForKey("evolveTo");
        items.addObject(value)
    }

}

The object is correct before the addObject call, but is incorrect after it

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("********************************************************************")
}

Gives
Pet CD: Optional("Mel")
Pet CD: Optional("Joan")

Below are the full classes

Setup.swift
import UIKit
import 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 - core data properties 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?

}

Pet
import Foundation
import CoreData


class Pet: NSManagedObject {

    func addEvolveToObject(value:Evolution) {
        let items = mutableSetValueForKey("evolveTo") as NSMutableSet;
        items.addObject(value)
    }

}

Evolution
import Foundation
import CoreData

extension Evolution {

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

}

The pet class can take multiple evolution objects, each evolution object is for one pet
Tagged:

Replies

  • Duncan CDuncan C Posts: 9,112Tutorial Authors, Registered Users @ @ @ @ @ @ @
    TL;DR
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • desbrinadesbrina HertfordshirePosts: 55Registered Users @
    Found out its to do with the relationships, if i have an inverse set i get the indue, if i don't then it works fine
Sign In or Register to comment.