You should use both enum
and struct
here:
// Enum with associated values for differences
enum FileType {
case withDate(date: Date)
case withImage(image: UIImage)
}
// Struct to bind common
struct File {
var name: String
var fileType: FileType
}
And then you can have your files defined:
extension File {
// Files with date
static let a = File(name: "a", fileType: .withDate(date: Date()))
static let b = File(name: "b", fileType: .withDate(date: Date()))
// Files with image
static let c = File(name: "c", fileType: .withImage(image: UIImage()))
static let d = File(name: "d", fileType: .withImage(image: UIImage()))
}
And for simple way to access date
and image
you can declare those handy extensions:
extension FileType {
var date: Date? {
if case let .withDate(date) = self {
return date
} else {
return nil
}
}
var image: UIImage? {
if case let .withImage(image) = self {
return image
} else {
return nil
}
}
}
extension File {
var date: Date? {
return fileType.date
}
var image: UIImage? {
return fileType.image
}
}
And then use it like this:
let date = File.a.date