Core Data: How do I store a custom object as a transformable attribute?

1.9k Views Asked by At

Currently I have a class called Place defined as follows:

class Place  {

    let name: String
    let address: String
    let coordinate: CLLocationCoordinate2D
    let type: String
    var photoReference: String?
    var photo: UIImage?

    /* functions etc */
}

In my data model, I have an entity, called FoundPlaces. It has one attribute, place, of type "transformable."

I am going insane trying to find the Swift solution to storing this object. Is the data model to begin with wrong?

Any guidance appreciated. Thanks!

1

There are 1 best solutions below

0
On

The object stored in the transformable attribute needs to implement the NSCoding protocol (init(coder:) and encodeWithCoder:).

The simplest way is to have the Place class inherit from NSObject and implement the methods above.