2

I have tried to look for a way for a long time and I couldn't find one that I can understand :(

I have a custom class that I made:

public class Player {
private var name : String = "Test"
private var level : Int = 1
private var skill : Skill = Skill("Basic") //Skill is another class
}

I'm creating an object from this class in my view controller:

var player = Player()

Now i wan't to save this player, so I have created an entity called Saves and an attribute called playerSaved and with transformable type.

When I save the player to playerSaved I get a crash "unrecognized selector sent to instance".

Thank you for your help :) Hope to get done with this and finish my app!

1 Answers1

-3

You need to transform your custom class into a NSManagedObject:

import CoreData

@objc(Player)
class Player: NSManagedObject {
    @NSManaged var name: String
    @NSManaged var level: NSNumber
    @NSManaged var skill: Skill
}

This is the standard way to save "custom" classes in Core Data.

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Skill will not be able to be saved. Because it is not a basic Obj-C type. How does one get around this? – Aaron Apr 23 '16 at 15:18
  • `NSManagedObject` descends from `NSObject`, so it **is** a "basic Objective-C type". – Mundi Apr 23 '16 at 22:41