Created
January 7, 2017 19:33
-
-
Save wh1pch81n/b953f2e35b5510a9759982ab4c1a4b30 to your computer and use it in GitHub Desktop.
NS_ENUM_STRING ports a struct with static strings. This means between objc and swift you have to learn how to read both versions of it which can be confusing. It is much better to have a more consistent api between both. Therefore you can use a class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func ==(lhs: EnumString, rhs: String) -> Bool { | |
| return lhs.isEqual(rhs) | |
| } | |
| func ==(lhs: String, rhs: EnumString) -> Bool { | |
| return rhs.isEqual(lhs) | |
| } | |
| class EnumString: NSObject { | |
| let rawValue: String | |
| init(rawValue: String) { | |
| self.rawValue = rawValue | |
| } | |
| //override var hashValue: Int { return rawValue.hashValue } | |
| override func isEqual(_ object: Any?) -> Bool { | |
| if let rhs = object as? EnumString { | |
| return self.rawValue == rhs.rawValue | |
| } else if let raw_rhs = object as? String { | |
| return self.rawValue == raw_rhs | |
| } else { | |
| return false | |
| } | |
| } | |
| static let America = EnumString(rawValue: "America") | |
| static let Canada = EnumString(rawValue: "Canada") | |
| } | |
| EnumString.America.rawValue | |
| EnumString.America == EnumString.America | |
| EnumString.America == EnumString(rawValue: "America") | |
| EnumString.America.isEqual(EnumString(rawValue: "America")) | |
| EnumString.America.isEqual("America") | |
| EnumString.America == "America" | |
| "America" == EnumString.America |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment