What is `<Value>` in `enum Result<Value> { ... }`? - ios

There's a enum like this:
enum Result<Value> {
case Success(Value)
case Failure(NSData?, ErrorType)
}
What's the <Value> part? It's not explained in the Apple documentation.

It's a generic, here is the doc references
What is means is when you create a variable of Result you pass the type in < > brackets
e.g.
var res: Result<String>
Now your Value becomes String
And if your want that to be Int then you will probably do like this
var res: Result<Int>

It's not written in the enum part of the Swift documentation, it's in generics:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html#//apple_ref/doc/uid/TP40014097-CH26-ID179

Related

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 - how to declare variable/functon of/with enums of different type?

I need to declare variable which will store array of enums of different type, eg.:
var enums = [EnumTypeA.Option1, EnumTypeB.Option2]
Compiler states:
Type of expression is ambiguous without more context
This will be necessary to pass any enum or other object as a function parameter. However I discovered that I can pass generics to achieve this, eg.:
func f1<T>(enum: T)
but having protocol with optional methods (prefixed with #objc) it is impossible.
You can use a protocol...
protocol MyEnums {}
enum MyEnum1: MyEnums {
case first, second
}
enum MyEnum2: MyEnums {
case red, green
}
let myArray: [MyEnums] = [MyEnum1.first, MyEnum2.green]
func myFunc(myEnum: MyEnums) {
print(myEnum)
}
for value in myArray {
myFunc(myEnum: value)
}
This was fun. Instead of generics, I just went with Any, since that is the base of everything.
enum TypeA {
case Option1
case Option2
}
enum TypeB {
case Option1
case Option2
}
func acceptDifferentEnums(value: Any) {
switch value {
case let typeA as TypeA:
print("This is TypeA")
case let typeB as TypeB:
print("This is typeB")
default:
print("This is something else")
}
}
acceptDifferentEnums(TypeA.Option1) // This is TypeA
acceptDifferentEnums(TypeB.Option2) // This is TypeB
acceptDifferentEnums("Foo") // This is something else
You then use the switch statement to downcast the value property into your various enums, and process them accordingly.

Swift: get self.description in self.description

I'm trying to modify description output for ErrorProtocol. But this snippet, gives me infinite loop.
enum GeneralError: ErrorProtocol, CustomStringConvertible {
case NoMemory
var description: String {
return String(self).lowercased()
}
}
Changing to self.dynamic type gives me a "generalerror".
Is there any way how to get just "nomemory"? Without using conditionals.
Swift: Convert enum value to String? isn't an exact match for your question, but it really covers the guts of it. In your case, you'll want to change it up a little bit:
enum Error : String, ErrorProtocol, CustomStringConvertible {
case NoMemory
var description : String {
return self.rawValue.lowercaseString
}
}
This works because the default rawValue for enum cases where the associated type is String is the name of the case.
When you call String(self) in GeneralError.description, you're making the String initializer use your object (self) as a CustomStringConvertible to make a new string. Internally, it does this by calling description on it, hence the recursion
What exactly are you trying to achieve?

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