Update / change the rawValue of a enum in Swift - ios

Taking the below enum for instance
enum Name : String {
case Me = "Prakash"
case You = "Raman"
}
Can I do the following
Change the raw value of one "case" to something else.
Name.Me = "Prak"
Add a new case to the ENUM
Name.Last = "Benjamin"
Thanks!

Short answer: No, you can't.
Enumeration types are evaluated at compile time.
It's not possible to change raw values nor to add cases at runtime.
The only dynamic behavior is using associated values.
Reference: Swift Language Guide: Enumerations

No you cannot. Instead You can redefine your enum to contain associated values instead of raw values.
enum Name {
case Me(String)
case You(String)
case Last(String)
}
var me = Name.Me("Prakash")
print(me)
me = .You("Raman")
print(me)
me = .Last("Singh")
print(me)

Related

Passing all Enum values in Swift

I have to pass an array of an enum as a function parameter, most of the time I have to pass every single case of the enum. Is there a shorthand way to include all instead of having to pass [.NFL, .NBA, .NHL, .MLB, .NCAAM, .NCAAF] every time? Can I make and all property so that I can just pass .all that included all the cases?
enum LSLeague: String {
case NFL = "NFL"
case NBA = "NBA"
case NHL = "NHL"
case MLB = "MLB"
case NCAAM = "NCAAM"
case NCAAF = "NCAAF"
}
You can use .allCases on the enum type.
So, you might still have to do a bit of work depending on your use case, but you can iterate over each element in an enum if you use:
for league in LSLeague.allCases {
//code here
}
EDIT: #aheze had a great point that I just wanted to add into this answer. He says, "Make sure to also conform the enum to CaseIterable"
So, also make sure that you change the enum you have to:
enum LSLeague: String: CaseIterable {
case NFL = "NFL"
case NBA = "NBA"
case NHL = "NHL"
case MLB = "MLB"
case NCAAM = "NCAAM"
case NCAAF = "NCAAF"
}
if you're able to use this technique.
Swift 5
Use CaseIterable protocol to the enum:
enum CustomTypes: String, CaseIterable {
case location = "Location"
case organization = "Organization"
case dutyUse = "Duty Use"
}
Usage:
let values: [String] = CustomTypes.allCases.map { $0.rawValue }
// values = ["Location", "Organization", "Duty Use"]

Enum Types: get raw value from key

How to get the raw value from a enum passing the key value? Must work for any enum types working like an extension of enum types.
Any mirror reflection, mappable or RawRepresentable solutions are welcome.
I would like something like this:
enum Test : String {
case One = "1"
}
Test.rawValueFromKey(keyValue: "One") // Must return "1"
// I don't want the solution below, I must get the rawValue through the key name as String.
Test.One.rawValue
I do need to get the rawValue passing the name of the key as a String. Otherwise, I will need to make a switch or many if/else conditions. I have a big Enum and I don't want to check the string passed in a switch for example and pass Test.One.rawValue. I would like to get the rawValue directly through the key as String, just like in a dictionary.
I also don't want the code below, because I have a Enum of 40 items and I think it is ugly to make a switch of 40 items.
switch keyValue {
case "One":
return Test.One.rawValue
break
}
I want something like this:
func rawValueFromKey (keyValue: String) -> Test {
// code here to get the rawValue through the keyValue as String
// return the proper Test Enum
}
I tried some possible solutions using Mirror reflection and enum iterations to find the rawValue through the keyValue but didn't work.
Please give the solution in both Swift 2 and 3.
Thanks
As far as I know, you can't reflect on types so I think you will be able to achieve this only with CustomReflectable or maybe using localized strings for mapping the values/keys. Invariably you'll have to map somehow.
Why not something like this? Sure you are just comparing Strings so it's potentially unsafe (if your enum would conform to CustomStringConvertible, etc), but you can't really avoid that.
I think CaseIterable is available only from Swift 4 though...
protocol KeyRepresentable: CaseIterable {
}
extension KeyRepresentable {
static func fromKey(key: String) -> Self? {
return Self
.allCases
.first { "\($0)" == key }
}
}
Usage:
enum Option: Int, KeyRepresentable {
case a = 1, b = 2
}
print(Option.fromKey(key: "a")?.rawValue)

Trying to work with an enum that has duplicate values in Swift

I have an enum of type String which needs to have multiple variables that have the same value. My enum looks something like this:
class MyClass {
enum MyEnum: String {
case blahA = "blaha"
case blahB = "blahb"
...
static var blahD = "blah"
static var blahE = "blah"
}
}
The reason why I'm using static var's in the above construction is because both "blahD" and "blahE" need to reference the same String value, used in different places (don't ask me why, it just has to be this way). However, I have a method where I need to pass in the value of the enum as follows:
if let testString = myString(foo: MyEnum.blahD) {...}
I unfortunately am getting the following compilation error:
Cannot convert value of type "String" to expected argument type "MyClass.MyEnum".
How do I get around passing the above variable which has duplicate values in my enum in the method, but cast it to the type of "MyClass.MyEnum"?
You can do this if you make the extra case reference the other enum case directly instead of just assigning them the same string value:
class MyClass {
enum MyEnum: String {
case blahA = "blaha"
case blahB = "blahb"
...
case blahD = "blah"
static var blahE = MyEnum.blahD
}
}
Then you can pass MyEnum.blahE the same way you would pass MyEnum.blahD
If the function takes a value of type MyEnum, you cannot do this. The type properties blahD and blahE are simply not of that type. Only the cases of the enum are of type MyEnum.
The function parameter's type must be changed to String.
The only other way around it would be to add a case to the enum that has a raw value matching the value of those two properties: case blahDOrE = "blah". Then you could construct that case: MyEnum(rawValue: MyEnum.blahD), but I can't see that being very useful.

Swift: Unable to switch on enum associated value

I'm converting an old Objective C app into SWIFT for learning purposes, and I've stumbled upon a strange problem when trying to provide a switch statement on an enum.
The code looks like this:
switch entry.mood {
case let Mood.THDiaryMoodGood:
self.moodImageView.image = UIImage(named: "icn_happy")
case let Mood.THDiaryMoodAverage:
self.moodImageView.image = UIImage(named: "icn_average")
case let Mood.THDiaryMoodBad:
self.moodImageView.image = UIImage(named: "icn_bad")
default:
self.moodImageView.image = UIImage(named: "icn_happy")
}
Where Mood is:
enum Mood: Int16 {
case THDiaryMoodGood = 0
case THDiaryMoodAverage = 1
case THDiaryMoodBad = 2
}
And the representation in mood is stored in a CoreData entity named mood with the type Integer 16.
My casts directly matched each-other, however when I try and use the switch statement provided above I get the error: Enum case pattern cannot match values of the non-enum type Int16.
I'm rather confused as to why I'm receiving this error, from my understanding the process should be evaluated like this:
entry.mood = 1
switch(1) {
// Int16: 0 returned from enum - would evaluate false and fall through to case B
case Mood.THDiaryMoodGood:
self.mood.image = ...
// Int16: 1 returned from enum - would evaluate true and set the icon
case Mood.THDiaryMoodAverage:
self.mood.image = ...
// Other cases not evaluated as we returned true...
}
Is my thought process or logic flawed here? I'm very confused...any help would be greatly appreciated, thanks!
The problem is that you’re passing an Int16 value to the switch. You’re setting entry.mood, an Int16, to the raw value 1, but the switch wants your Mood type. So you have a type mismatch.
You can solve it by turning the value into a Mood:
switch Mood(rawValue: entry.mood)! {
Swift enums are not the same as Objective-C enums. In Objective-c, the enum is the backing value, referred to by a different name. In swift, the enum is it's own type, and the value is an associated value (you don't actually need a backing value if you don't want one, in your case it makes sense though).
Have a read of this, it explains things nicely. https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html

Set enum from NSDictionary Swift

I have information stored in a plist file that I pull into a dictionary.
I have a class with some enums set up as follows:
enum componentPostion {
case upperLeft, UpperRight, lowerLeft, lowerRight
}
I've declared a var as of type componentPostion
var isPosition: componentPostion
Can I then set the enum from the value in the dictionary without having to write a function with a switch statement etc. I've tried this with no luck
isPosition = componentInfo["Type"] as componentPostion
You can use raw values by inheriting the enum from the type you want to hold, in your case I presume it's string:
enum componentPostion : String{
case upperLeft = "upperLeft"
case upperRight = "upperRight"
case lowerLeft = "lowerLeft"
case lowerRight = "lowerRight"
}
Then you can use fromRaw() to obtain an enum case:
let isPosition = componentPostion.fromRaw("upperLeft")
and toRaw() to obtain its string representation
isPosition.toRaw()
Note that fromRaw() returns an optional, in case the parameter doesn't match any raw value defined for the enum
Suggested reading: Raw Values

Resources