How to convert custom object to Data Swift

假装没事ソ 提交于 2021-01-27 20:05:22

问题


I have a custom struct that has properties of types that are other structs with non codable types. It is a complex tree of structs within structs, and I need to convert it to Data so I can save it.

I need to save this struct (chat)

struct Chat{
    var dictOfRecentMessages:[String:Message] = [:]
    var matchPicture:UIImage? 
    var metadata:JSON?
}

    struct Message{
        let messageID:String
        var chatID:String
        let whoSent:String
        let timeTriedToSend:Int


        var messageString:String?
        var image:ImageMessage?
        var video:VideoMessage?
        var location:LocationMessage?
        let status:String 

    }

struct ImageMessage{
    var uiImage:UIImage?
    var imageURL:String?
    var imageSize:CGSize?
}
struct VideoMessage{
    var video:NSURL?
    var videoURL:String?
}
struct LocationMessage{
    var clLocation:CLLocation?
    var mapCamera:MKMapCamera?
    var mapSnapshot:MKMapSnapshot?
}

Is serialization the best way to go, like in this post? Swift structs to NSData and back

I have tried conforming to Codable but it gives an error saying it does not conform, which is expected due to the types.

来源:https://stackoverflow.com/questions/51211262/how-to-convert-custom-object-to-data-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!