核心数据:如何将自定义对象存储为可转换属性? [英] Core Data: How do I store a custom object as a transformable attribute?

查看:73
本文介绍了核心数据:如何将自定义对象存储为可转换属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个名为Place的类,定义如下:

  class Place {

let名称:字符串
出租地址:字符串
出租坐标:CLLocationCoordinate2D
出租类型:字符串
var photo参考:字符串?
var照片:UIImage?

/ *函数等* /
}

在我的数据模型中,我有一个名为FoundPlaces的实体。它有一个属性 place ,类型为 transformable。



我会很疯狂地试图找到Swift解决此对象的方法。数据模型会以错误开头吗?



任何指导都值得赞赏。

解决方案

存储在transformable属性中的对象需要实现 NSCoding 协议( init(coder:) encodeWithCoder:)。



最简单的方法是让 Place 类从 NSObject 继承并实现上述方法。 / p>

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!

解决方案

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.

这篇关于核心数据:如何将自定义对象存储为可转换属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆