问题
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