@ Antonio gives a workaround, but does not answer the actual question.
Define your listing.
enum FileType { case Image, Video }
Give cases non-literal values, regardless of the type you want, with the RawRepresentable protocol. Do this with the enum extension to have cleaner code.
extension FileType: RawRepresentable { typealias Tuple = (name: String, contentTypeMatcher: String) private static let allCases = [FileType.Image, .Video] // MARK: RawRepresentable typealias RawValue = Tuple init?(rawValue: Tuple) { guard let c = { () -> FileType? in for iCase in FileType.allCases { if rawValue == iCase.rawValue { return iCase } } return nil }() else { return nil } self = c } var rawValue: Tuple { switch self { case .Image: return Tuple("Image", "image/") case .Video: return Tuple("Video", "video/") } } }
To be able to map Tuple in the switch, implement the pattern matching operator.
private func ~= (lhs: FileType.Tuple, rhs: FileType.Tuple) -> Bool { return lhs.contentTypeMatcher == rhs.contentTypeMatcher && lhs.name == rhs.name }
And here it is ...
let a = FileType.Image print(a.rawValue.name) // "Image" let b = FileType(rawValue: a.rawValue)! print(a == b) // "true" print(b.rawValue.contentTypeMatcher) // "image/"
Say I answered a question without interrogation. Now ... Enums (at least in Swift) are for unique occasions. The caveat about this workaround is that you can (I hope by chance) stick with the same rawValue for more cases. Usually your code code smells to me. If you (for a very reasonable reason) need to create a new enum value from a tuple, consider a redesign. If you want to go with this workaround, I suggest (depending on the project) to implement some verification if all the original case values ββare unique. If not, consider this:
enum FileType { case Video, Image var name: String { switch self { case .Image: return "Image" case .Video: return "Video" } var contentTypeMatcher: String { switch self { case .Image: return "image/" case .Video: return "video/" } }
Stanislav Smida
source share