Reputation: 875
I want to write a function for saving my struct Array. I know, that using NSCoding is the right way, but only available to classes. But I found a workaround using a class including the struct itself:
extension EXIFData {
class EXIFDataClass: NSObject, NSCoding {
var exifData: EXIFData?
...
}
But how do I use it (call it)? E.g. defined as
var exif : EXIFData
or (next step)
var exifs : [EXIFData]
I like to use as much as possible of the actual features of Swift 3.0, because I'm just starting. I found some different solutions for older styles and versions of Swift.
Upvotes: 0
Views: 283
Reputation: 3789
I am not sure what your class EXIFData is, but I think that you are better off saving a class that contains a EXIFData object.
Say for example you need to save a bunch of different types of data including EXIFData, then you could create a class like the following which decodes, encodes, and initializes a class with an EXIFData type:
class UserData: NSObject, NSCoding {
var exifData: EXIFData
//other variables of data....
required init?(coder aDecoder: NSCoder) {
exifData = aDecoder.decodeObject(forKey: "EXIFData") as! EXIFData
//decode other values
}
init(exifData: EXIFData) {
self.exifData = exifData
//intialize other values
}
func encode(with aCoder: NSCoder) {
aCoder.encode(exifData, forKey: "EXIFData")
//encode other values
}
}
Then you could initialize your class like so:
var myUserData = UserData(...) //initialize with EXIFData
And then call your user "EXIFData" like so:
myUserData.exifData
Upvotes: 1