Is there any way to copy all the values of an enumeration into a dictionary without polling them in any FOR loop?
For example, from this enumeration:
enum FruitPriority: String {
case PEARS
case ORANGES
case APPLES
}
to call some single function from within the ENUM, something like this:
var fruitPriorityArray: [String : Int] = FruitPriority.someFunction()
and get this result (sorted according to hash value):
["PEARS": 0, "ORANGES": 1, "APPLES": 2]
The preference would be to make only a single call to the ENUM.
Thank you.
enum FruitPriority: String, CaseIterable {
case PEARS
case ORANGES
case APPLES
}
let result = FruitPriority.allCases.enumerated().reduce([String: Int]()) { dict, fruit in
var dict = dict
dict[fruit.element.rawValue] = fruit.offset
return dict
}
print(result)
The result is:
["PEARS": 0, "ORANGES": 1, "APPLES": 2]
For version Swift 4.1 and earlier the implementation of CaseIterable:
#if swift(>=4.2)
#else
public protocol CaseIterable {
associatedtype AllCases: Collection where AllCases.Element == Self
static var allCases: AllCases { get }
}
extension CaseIterable where Self: Hashable {
static var allCases: [Self] {
return [Self](AnySequence { () -> AnyIterator<Self> in
var raw = 0
return AnyIterator {
let current = withUnsafeBytes(of: &raw) { $0.load(as: Self.self) }
guard current.hashValue == raw else {
return nil
}
raw += 1
return current
}
})
}
}
#endif
Original post of CaseIterable implementation.
If you need just single call to the enum:
Add CaseIterable to your enum and then just create function getDictionary in enum which returns you dictionary (for each enum case will be rawValue assigned as key and hashValue as value )
enum FruitPriority: String, CaseIterable {
case PEARS
case ORANGES
case APPLES
func getDictionary() -> [String: Int] {
var dictionary = [String: Int]()
FruitPriority.allCases.forEach {
dictionary[$0.rawValue] = $0.hashValue
}
return dictionary
}
}
then you can just call
var fruitPriorityArray: [String : Int] = FruitPriority.getDictionary()
Note: if you're using earlier versions of Swift you can see this to create CaseIterable protocol
If you make your enum CaseIterable, you can construct a dictionary using reduce(into:). Since the hashValue can now change from run to run, I would recommend using enumerated() to number the cases in order:
enum FruitPriority: String, CaseIterable {
case PEARS
case ORANGES
case APPLES
}
let result = FruitPriority.allCases.enumerated().reduce(into: [:]) { $0[$1.element.rawValue] = $1.offset }
print(result)
// ["ORANGES": 1, "APPLES": 2, "PEARS": 0]
Related
I am new on swift. I have 2+ enums and i would like to create enum to object generator function. I couldn't find what type I should use for the Enum parameter in my function.
My enums;
public enum Animal: String, CaseIterable {
case DOG = "dog"
case CAT = "cat"
case BIRD = "bird"
}
public enum Car: String, CaseIterable {
case BMW = "bmw"
case AUDI = "audi"
}
My Function;
func enumToObj(Enum:TYPE?) -> Dictionary <String, String> {
var enumsObject: [String:String] = [:];
for enumData in Enum.allCases {
let value = enumData.rawValue;
enumsObject[value] = value;
}
return enumsObject;
}
//my expectation
enumToObj(Animal) && enumToObj(Car)
You can define a generic function which take as argument a type that is CaseIterable and RawRepresentable:
func enumToObj<T>(_ Enum:T.Type) -> [String: T.RawValue]
where T: CaseIterable & RawRepresentable
{
var enumsObject: [String: T.RawValue] = [:];
for enumData in Enum.allCases {
let value = enumData.rawValue;
enumsObject["\(enumData)"] = value;
}
return enumsObject;
}
print(enumToObj(Animal.self))
// ["CAT": "cat", "DOG": "dog", "BIRD": "bird"]
print(enumToObj(Car.self))
// ["AUDI": "audi", "BMW": "bmw"]
Using reduce(into:_:) this can also be written as
func enumToObj<T>(_ Enum:T.Type) -> [String: T.RawValue]
where T: CaseIterable & RawRepresentable
{
return Enum.allCases.reduce(into: [:]) { (enumsObject, enumData) in
let value = enumData.rawValue;
enumsObject["\(enumData)"] = value;
}
}
Given the following codable struct:
struct MyBanana: Codable {
var b: String?
var z: String?
enum CodingKeys: String, CodingKey {
case b = "B"
case z = "ZOOM"
}
}
How can i get an array of strings like so: ["B", "ZOOM"] without an instance of MyBanana?
Additionally, is there a way to get this from auto-synthesised CodingKeys, e.g.
struct MyBanana: Codable {
var b: String?
var z: String?
}
would return ["b","z"]
I tried the following for the first question:
func whatever<T, Key>(keyedBy: Key.Type) -> [T] where T: Codable, Key: CodingKey
{
let allKeys: [Key] = [Key]()
for k in keyedBy {
allKeys.append(k)
}
return [T]()
}
But i get
Type 'Key.Type' does not conform to protocol 'Sequence'
Update
For declared CodingKeys #Cameron Porter's answer works fine (add CaseIterable to the enum)
For synthesized ones this works, albeit pretty manual still:
extension Decodable {
func getAllCodingKeys(_ activator: (String) -> String) -> [String] {
let mirror = Mirror(reflecting: self)
return mirror.children.filter({ $0.label != nil }).map({ activator($0.label!) })
}
}
Then in your Codable, add
func getAllKeys() -> [String] {
return self.getAllCodingKeys { CodingKeys(stringValue: $0)!.stringValue }
}
struct MyBanana: Codable {
var b: String?
var z: String?
enum CodingKeys: String, CodingKey, CaseIterable {
case b = "B"
case z = "ZOOM"
}
static var allCases: [String] {
return CodingKeys.allCases.map { $0.rawValue }
}
}
And then you can get your array of coding keys as
MyBanana.allCases
Doesn't work for auto-synthesised coding keys however
I have the following enum
enum MoneyCupUsersBackEndRouter: URLRequestConvertible {
case getInfo
case postUserConnection(ConnectionData)
case postPersonalInfo(UserUpdatePersonalInformationsRequest)
case postKycAnswers(QuestionnaireAnswers)
switch self {
case .postUserConnection(let parameters):
let r = parameters.encode()
print(r)
case .postPersonalInfo(let parameters):
let r = parameters.encode()
print(r)
case .postKycAnswers(let parameters):
let r = parameters.encode()
print(r)
default:
break
}
This code is quite ugly, I had to duplicate the cases in the switch since each time, parameters are of a different type. But all of the paramaters follow the 'Codable' protocole.
There must be a way to avoid that by using some sort of generic types.
All the encode functions are declared like the following one :
func encode() -> [String: Any] {
return ["id": id, "data": data]
}
The returned dictionnary contains the struc fields.
If all associated types conform to a common protocol Foo (which has the encode()
method as a requirement) then you can use the as Foo pattern for the associated values to combine all cases to a single one.
Here is a self-contained example (tested with Xcode 10/Swift 4.2):
protocol Foo {
func encode() -> [String: Any]
}
class A: Foo {
func encode() -> [String: Any] { return ["A": 1] }
}
class B: Foo {
func encode() -> [String: Any] { return ["B": 2] }
}
class C: Foo {
func encode() -> [String: Any] { return ["C": 3] }
}
enum MyEnum {
case a(A)
case b(B)
case c(C)
func test() {
switch self {
case .a(let parameters as Foo),
.b(let parameters as Foo),
.c(let parameters as Foo):
let r = parameters.encode()
print(r)
}
}
}
MyEnum.a(A()).test() // ["A": 1]
MyEnum.b(B()).test() // ["B": 2]
MyEnum.c(C()).test() // ["C": 3]
If all you need is an encodable parameter, you could try this -
case getInfo
case postUserConnection(Codable)
case postPersonalInfo(Codable)
case postKycAnswers(Codable)
Then -
func stuff() {
switch self {
case .postUserConnection(let parameters), .postPersonalInfo(let parameters), .postKycAnswers(let parameters):
let r = try? parameters.encode()
print(r as Any)
default:
break
}
}
But of course you may need the different types you currently have as the associated values, then this wouldn't be possible.
I have tried to boil this issue down to its simplest form with the following.
Setup
Xcode Version 6.1.1 (6A2008a)
An enum defined in MyEnum.swift:
internal enum MyEnum: Int {
case Zero = 0, One, Two
}
extension MyEnum {
init?(string: String) {
switch string.lowercaseString {
case "zero": self = .Zero
case "one": self = .One
case "two": self = .Two
default: return nil
}
}
}
and code that initializes the enum in another file, MyClass.swift:
internal class MyClass {
let foo = MyEnum(rawValue: 0) // Error
let fooStr = MyEnum(string: "zero")
func testFunc() {
let bar = MyEnum(rawValue: 1) // Error
let barStr = MyEnum(string: "one")
}
}
Error
Xcode gives me the following error when attempting to initialize MyEnum with its raw-value initializer:
Cannot convert the expression's type '(rawValue: IntegerLiteralConvertible)' to type 'MyEnum?'
Notes
Per the Swift Language Guide:
If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value’s type (as a parameter called rawValue) and returns either an enumeration member or nil.
The custom initializer for MyEnum was defined in an extension to test whether the enum's raw-value initializer was being removed because of the following case from the Language Guide. However, it achieves the same error result.
Note that if you define a custom initializer for a value type, you will no longer have access to the default initializer (or the memberwise initializer, if it is a structure) for that type. [...]
If you want your custom value type to be initializable with the default initializer and memberwise initializer, and also with your own custom initializers, write your custom initializers in an extension rather than as part of the value type’s original implementation.
Moving the enum definition to MyClass.swift resolves the error for bar but not for foo.
Removing the custom initializer resolves both errors.
One workaround is to include the following function in the enum definition and use it in place of the provided raw-value initializer. So it seems as if adding a custom initializer has a similar effect to marking the raw-value initializer private.
init?(raw: Int) {
self.init(rawValue: raw)
}
Explicitly declaring protocol conformance to RawRepresentable in MyClass.swift resolves the inline error for bar, but results in a linker error about duplicate symbols (because raw-value type enums implicitly conform to RawRepresentable).
extension MyEnum: RawRepresentable {}
Can anyone provide a little more insight into what's going on here? Why isn't the raw-value initializer accessible?
This bug is solved in Xcode 7 and Swift 2
extension TemplateSlotType {
init?(rawString: String) {
// Check if string contains 'carrousel'
if rawString.rangeOfString("carrousel") != nil {
self.init(rawValue:"carrousel")
} else {
self.init(rawValue:rawString)
}
}
}
In your case this would result in the following extension:
extension MyEnum {
init?(string: String) {
switch string.lowercaseString {
case "zero":
self.init(rawValue:0)
case "one":
self.init(rawValue:1)
case "two":
self.init(rawValue:2)
default:
return nil
}
}
}
You can even make the code simpler and useful without switch cases, this way you don't need to add more cases when you add a new type.
enum VehicleType: Int, CustomStringConvertible {
case car = 4
case moped = 2
case truck = 16
case unknown = -1
// MARK: - Helpers
public var description: String {
switch self {
case .car: return "Car"
case .truck: return "Truck"
case .moped: return "Moped"
case .unknown: return "unknown"
}
}
static let all: [VehicleType] = [car, moped, truck]
init?(rawDescription: String) {
guard let type = VehicleType.all.first(where: { description == rawDescription })
else { return nil }
self = type
}
}
Yeah this is an annoying issue. I'm currently working around it using a global-scope function that acts as a factory, i.e.
func enumFromString(string:String) -> MyEnum? {
switch string {
case "One" : MyEnum(rawValue:1)
case "Two" : MyEnum(rawValue:2)
case "Three" : MyEnum(rawValue:3)
default : return nil
}
}
This works for Swift 4 on Xcode 9.2 together with my EnumSequence:
enum Word: Int, EnumSequenceElement, CustomStringConvertible {
case apple, cat, fun
var description: String {
switch self {
case .apple:
return "Apple"
case .cat:
return "Cat"
case .fun:
return "Fun"
}
}
}
let Words: [String: Word] = [
"A": .apple,
"C": .cat,
"F": .fun
]
extension Word {
var letter: String? {
return Words.first(where: { (_, word) -> Bool in
word == self
})?.key
}
init?(_ letter: String) {
if let word = Words[letter] {
self = word
} else {
return nil
}
}
}
for word in EnumSequence<Word>() {
if let letter = word.letter, let lhs = Word(letter), let rhs = Word(letter), lhs == rhs {
print("\(letter) for \(word)")
}
}
Output
A for Apple
C for Cat
F for Fun
Add this to your code:
extension MyEnum {
init?(rawValue: Int) {
switch rawValue {
case 0: self = .Zero
case 1: self = .One
case 2: self = .Two
default: return nil
}
}
}
Is this possible to create a enum of Tuples in Swift?
I'd like to build something like:
enum ErrorCode: (Int, String) {
case Generic_Error = (0, "Unknown")
case DB_Error = (909, "Database")
}
But it doesn't compile... Is there a way to obtain a similar result?
Swift enumerations cannot have Tuples as a raw value type.
Alternative approaches include storing the code and deriving a description from that:
enum ErrorCode: Int, CustomStringConvertible {
case Generic = 0
case DB = 909
var description: String {
switch self {
case .Generic:
return "Unknown"
case .DB:
return "Database"
}
}
}
...or storing associated values for code and description in the enumeration cases themselves:
enum Error {
case Generic(Int, String)
case DB(Int, String)
}
If you're just looking for constant values, #matt's suggestion of organizing them within a struct would work, too.
It depends what you mean by "similar". What I do is use a Struct with static constant properties:
struct Trouble {
static let Generic_Error = (0, "Unknown")
static let DB_Error = (909, "Database")
}
Now things like Trouble.Generic_Error are usable throughout your code.
You can conform to RawRepresentable and use (Int, String) as the Self.RawValue associated type.
enum ErrorCode: RawRepresentable {
case Generic_Error
case DB_Error
var rawValue: (Int, String) {
switch self {
case .Generic_Error: return (0, "Unknown")
case .DB_Error: return (909, "Database")
}
}
init?(rawValue: (Int, String)) {
switch rawValue {
case (0, "Unknown"): self = .Generic_Error
case (909, "Database"): self = .DB_Error
default: return nil
}
}
}
you can do such thing, maybe:
enum ErrorCode {
case Generic_Error
case DB_Error
func values() -> (code: Int!, description: String?)! {
switch self {
case .Generic_Error:
return (0, "Unknown")
case .DB_Error:
return (909, "Database")
}
}
}
and you can do such thing later:
let errorCode: ErrorCode = ErrorCode.Generic_Error;
if (errorCode.values().code == 0) {
// do the business here...
}
Create your enum and add a var of tuple type (String, String).
enum SomeType {
case type1
case type2
case type3
var typeNameAndDescription: (name: String, description: String) {
switch self {
case .type1:
return ("type1 name", "type1 description")
case .type2:
return ("type2 name", "type2 description")
case .type3:
return ("type3 name", "type3 description")
}
}
}
and later:
let myType = SomeType.type1
let typeName = myType.typeNameAndDescription.name
let typeDescription = myType.typeNameAndDescription.description
My solution to keep the enum, was to create the get method for the rawValue var:
enum LoadingType {
case poster
case movie
case refresh
var rawValue: (file: String, anim: String) {
get {
switch self {
case .movie:
return ("poster_loading", "LoadingView")
case .poster:
return ("loading", "LoadingView")
case .refresh:
return ("loading", "RefreshView")
}
}
}
}
With this code, you can even call each of your Tuple elements by a name:
self.type.rawValue.file
I think I would change your code to something like this:
enum ErrorCode {
case generic, db
var message: String {
switch self {
case .generic:
return "Unknown"
case .db:
return "Database"
}
}
var code: Int {
switch self {
case .generic:
return 0
case .db:
return 909
}
}
}
I feel this would make it much more easier to use like so:
let error = ErrorCode.generic
print("Error Code: \(error.code), Message: \(error.message)")
enum ErrorCode {
case Generic_Error
case DB_Error
public var code:Int{
switch self {
case .Generic_Error: return 0
case .DB_Error: return 909
}
}
public var name:String{
switch self {
case .Generic_Error: return "Unknown"
case .DB_Error: return "Database"
}
}
}
using:
let err:ErrorCode = .Generic_Error
print(err.code) //0
print(err.name) //Unknown
This is what I did in a similar situation. Use an associated data enum, with default values (you can even name the values in the tuple if you like). Then add a simple function that returns the tuple based on self.
enum ErrorCode {
case Generic_Error( code: Int = 0, desc: String = "Unknown")
case DB_Error( code: Int = 909, desc: String = "Database")
func getCodeDescription() -> (Int, String) {
switch self {
case let .Generic_Error(code, desc): return (code, desc)
case let .DB_Error(code, desc): return(code, desc)
}
}
}
Later
var errorCode: Int
var errorDesc: String
let myError: ErrorCode = .DB_Error() // let it take the default values
(errorCode, errorDesc) = myError.getCodeDescription()
Create an Enumeration as Int.
For example:
https://github.com/rhodgkins/SwiftHTTPStatusCodes/blob/master/Sources/HTTPStatusCodes.swift