Dictionary of Optional Keys - swift

Is there any workaround to create a dictionary with Optional keys? I know a proper solution for this would require Conditional Conformances to be implemented, but do I have any options before then?
let dict: [Int?: Int] = [nil: 1]
// ERROR: type 'Int?' does not conform to protocol 'Hashable'

Update: Swift 4.2
Conditional conformances have been implemented in Swift 4.2, woohoo! 🎉
This allowed for Optional to be made conditionally Hashable, when the wrapped element is Hashable. So you can use any Optional<T: Hashable> as a dictionary key, directly!
let d: [Int?: String] = [
nil: "nil",
1: "a",
2: "b",
3: "c",
]
for (key, value) in d {
let stringKey = key.map(String.init(describing:)) ?? "nil"
print("\(stringKey): \(value)")
}
Before Swift 4.2
Here's one workaround I found: Create a new HashableOptional enum:
enum HashableOptional<Wrapped: Hashable> {
case none
case some(Wrapped)
public init(_ some: Wrapped) {
self = .some(some)
}
public init(_ optional: Wrapped?) {
self = optional.map{ .some($0) } ?? .none
}
public var value: Wrapped? {
switch self {
case .none: return nil
case .some(let wrapped): return wrapped
}
}
}
extension HashableOptional: Equatable {
static func ==(lhs: HashableOptional, rhs: HashableOptional) -> Bool {
switch (lhs, rhs) {
case (.none, .none): return true
case (.some(let a), .some(let b)): return a == b
default: return false
}
}
}
extension HashableOptional: Hashable {
var hashValue: Int {
switch self {
case .none: return 0
case .some(let wrapped): return wrapped.hashValue
}
}
}
extension HashableOptional: ExpressibleByNilLiteral {
public init(nilLiteral: ()) {
self = .none
}
}
And then you can use it like so:
let dict: [HashableOptional<Int>: Int] = [nil: 1]

Related

Swift abstraction for enums with associated value

I have an enum with associated value ConfigurationValue that gets stored in a map. I have type specific getters for each possible enum value.
How do I minimize the code duplication between these getters
enum ConfigurationValue {
case bool(Bool)
case int(Int)
case string(String)
}
var map: [T:ConfigurationValue] = [:]
public mutating func set(key: T, _ value: Bool) {
map[key] = .bool(value)
}
public mutating func set(key: T, _ value: Int) {
map[key] = .int(value)
}
public mutating func set(key: T, _ value: String) {
map[key] = .string(value)
}
private func getValue(key: T) -> ConfigurationValue? {
return map[key]
}
public func get(key: T) -> Bool? {
guard let value = getValue(key: key),
case .bool(let innerValue) = value else {
return nil
}
return innerValue
}
public func get(key: T) -> Int? {
guard let value = getValue(key: key),
case .int(let innerValue) = value else {
return nil
}
return innerValue
}
public func get(key: T) -> String? {
guard let value = getValue(key: key),
case .string(let innerValue) = value else {
return nil
}
return innerValue
}
I would not use protocols but instead I would add two methods in ConfigurationValue the first to create a configuration value and the other to get the inner value of a case.
enum ConfigurationValue {
case bool(Bool)
case int(Int)
case string(String)
static func with<Value>(value: Value?) -> ConfigurationValue? {
if let value = value as? Bool {
return .bool(value)
}
if let value = value as? Int {
return .int(value)
}
if let value = value as? String {
return .string(value)
}
return nil
}
func get<Value>(ofType type: Value.Type = Value.self) -> Value? {
switch self {
case .bool(let bool):
return bool as? Value
case .int(let int):
return int as? Value
case .string(let string):
return string as? Value
}
}
}
let boolValue: Bool? = ConfigurationValue.bool(false).get() // Optional(false)
let intValue: Int? = ConfigurationValue.bool(false).get() // nil
let stringValue: String? = ConfigurationValue.bool(false).get() // nil
let boolConfiguration: ConfigurationValue? = .with(value: true) // bool(true)
let intConfiguration: ConfigurationValue? = .with(value: 42) // int(42)
let stringConfiguration: ConfigurationValue? = .with(value: "42") // string("42")
let doubleConfiguration: ConfigurationValue? = .with(value: 42.0) // nil
Now you can avoid boilerplate, let's say you have a struct Foo:
struct Foo<T: Hashable> {
var map: [T:ConfigurationValue] = [:]
mutating func set<Value>(_ value: Value?, forKey key: T) {
map[key] = .with(value: value)
}
func get<Value>(key: T) -> Value? {
guard let value = map[key] else {
return nil
}
return value.get()
}
}
var map: [String:ConfigurationValue] = [
"bool": .bool(false),
"int": .int(0),
"string": .string("string")
]
var bar = Foo(map: map)
var bool: Bool? = bar.get(key: "bool") // Optional(false)
var int: Int? = bar.get(key: "int") // 0
var string: String? = bar.get(key: "string") // string
bar.set(true, forKey: "bool")
bool = bar.get(key: "bool") // Optional(true)
bar.set(42, forKey: "int")
int = bar.get(key: "int") // 42
bar.set("Hello World!", forKey: "string")
string = bar.get(key: "string") // Hello World!
It doesn't seem like an enum is the right tool for the job here since you can get the same usefulness out of just casting the values (though admittedly you wouldn't get the benefit of exhaustive switch errors). If you're wanting to utilize generics to cut down on some of this duplication, I'd go with a wrapper around the map like so:
protocol Configurable {}
extension Int: Configurable {}
extension Bool: Configurable {}
extension String: Configurable {}
class ConfigurationMap<Key: Hashable> {
private var map = [Key: Configurable]()
func getValue(at key: Key) -> Configurable? {
return map[key]
}
func setValue(_ value: Configurable, at key: Key) {
map[key] = value
}
}
Here Key would replace your T generic. To incorporate more data types, just extend the type that you want to use to be Configurable. The ConfigurationMap can also be a struct if you don't mind the mutating methods.
In Swift, this is not called a "map", but instead, a "dictionary". Type restriction can be achieved with a protocol, and explicit typing for retrieval.
public protocol ConfigurationValue { }
extension Int: ConfigurationValue { }
extension Bool: ConfigurationValue { }
extension String: ConfigurationValue { }
public struct ConfigurationDictionary<Key: Hashable> {
private var dictionary: [Key: ConfigurationValue] = [:]
}
public extension ConfigurationDictionary {
subscript<Value: ConfigurationValue>(key: Key) -> Value? {
get { dictionary[key] as? Value }
set { dictionary[key] = newValue }
}
}
var configurationDictionary = ConfigurationDictionary<String>()
configurationDictionary["i"] = 1
configurationDictionary["i"] as Int? // 1
configurationDictionary["i"] as Bool? // nil
configurationDictionary["b"] = true
let b: Bool? = configurationDictionary["b"] // true
let i: Int? = configurationDictionary["b"] // nil

Testing for compliance with and casting to RawRepresentable protocol

I have some generic code that allows me to read and write various types to the defaults system, e.g. value getters and setters:
var value : T {
get {
if T.self == Int.self {
return UserDefaults.standard.integer(forKey: storageKey) as! T
} else if T.self == Double.self {
return UserDefaults.standard.double(forKey: storageKey) as! T
} else if T.self == Float.self {
return UserDefaults.standard.float(forKey: storageKey) as! T
} else if T.self == Bool.self {
return UserDefaults.standard.bool(forKey: storageKey) as! T
} else if T.self == String.self {
return UserDefaults.standard.string(forKey: storageKey) as! T
} else {
return UserDefaults.standard.value(forKey: self.storageKey) as! T
}
}
set(value) {
UserDefaults.standard.set(value, forKey: storageKey)
UserDefaults.standard.synchronize()
}
}
Now I want to add my own enum types to this mechanism by making them RawRepresentable<Int>, e.g.
enum Direction : Int, RawRepresentable {
case left = 0
case right = 1
}
Unfortunately, I can neither find the magic incantation to test whether T conforms to the RawRepresentable protocol, nor can I cast T to the RawRepresentable protocol, because no matter what I try, I always end up with a Protocol 'RawRepresentable' can only be used as a generic constraint because it has Self or associated type requirements.
I have tried every where and as incantation until I have started doubting that it can be done at all!?
I'm in Swift 5 and the goal is to create new instance by invoking CustomType(rawValue:) and getting the Int value by calling myValue.rawValue.
As #vadian said, all those type checks can be replaced be a single call to UserDefaults.standard.object() and conditional casting. Also the type of the value property needs to be an optional to handle the case where the property is not set (or not of the correct type):
struct DefaultKey<T> {
let storageKey: String
var value: T? {
get {
return UserDefaults.standard.object(forKey: storageKey) as? T
}
nonmutating set {
UserDefaults.standard.set(newValue, forKey: storageKey)
}
}
}
And then you can define a constrained extension method where you specialize the computed property for the case of RawRepresentable types:
extension DefaultKey where T: RawRepresentable {
var value: T? {
get {
if let rawValue = UserDefaults.standard.object(forKey: storageKey) as? T.RawValue {
return T(rawValue: rawValue)
}
return nil
}
nonmutating set {
UserDefaults.standard.set(newValue?.rawValue, forKey: storageKey)
}
}
}
Example usage:
enum Direction : Int {
case left = 0
case right = 1
}
let key1 = DefaultKey<Int>(storageKey: "foo")
key1.value = 123
let key2 = DefaultKey<Direction>(storageKey: "bar")
key2.value = .right
print(key1.value as Any) // Optional(123)
print(key2.value as Any) // Optional(Direction.right)
Note that this can still crash if used with non-property-list types. To be on the safe side, you would have to restrict the extensions to types which are known to be user defaults storable (integers, floats, strings, ...):
protocol UserDefaultsStorable {}
extension Int: UserDefaultsStorable {}
extension Float: UserDefaultsStorable {}
// ...
struct DefaultKey<T> {
let storageKey: String
}
extension DefaultKey where T: UserDefaultsStorable { .. }
extension DefaultKey where T: RawRepresentable, T.RawValue: UserDefaultsStorable { ... }

Extending swift optional for default values

I'm trying to extend Swift's Optional type with default values. Providing empty values in API requests should raise an exception. I've done this for the String type, but I can't achieve the same result with the Integer type:
extension Optional where Wrapped == String {
var unwrappedValue: String {
get {
switch self {
case .some(let value):
return value
case .none:
return ""
}
}
}
}
The Integer version is throwing the following Error:
Protocol 'Integer' can only be used as a generic constraint because it
has Self or associated type requirements
extension Optional where Wrapped == Integer {
var unwrappedValue: Integer {
get {
switch self {
case .some(let value):
return value
case .none:
return 0
}
}
}
}
If you use this for a lot of Types you might want to consider the following addition to the answer of Leo Dabus:
protocol Defaultable {
static var defaultValue: Self { get }
}
extension Optional where Wrapped: Defaultable {
var unwrappedValue: Wrapped { return self ?? Wrapped.defaultValue }
}
This way you can extend your types very easily:
extension Int: Defaultable {
static var defaultValue: Int { return 0 }
}
extension String: Defaultable {
static var defaultValue: String { return "" }
}
extension Array: Defaultable {
static var defaultValue: Array<Element> { return [] }
}
And usage goes like this:
let optionalInt: Int? = 10 // Optional(10)
let unwrappedInt = optionalInt.unwrappedValue // 10
let optionalString: String? = "Hello" // Optional("Hello")
let unwrappedString = optionalString.unwrappedValue // "Hello"
let optionalArray: [Int]? = nil // nil
let unwrappedArray = optionalArray.unwrappedValue // []
You just need to return Wrapped instead of Integer
extension Optional where Wrapped: Integer {
var unwrappedValue: Wrapped {
switch self {
case .some(let value):
return value
case .none:
return 0
}
}
}
or simply
extension Optional where Wrapped: Integer {
var safelyUnwrapped: Wrapped { return self ?? 0 }
}
let optionalInt = Int("10")
let unwrappedValue = optionalInt.safelyUnwrapped // 10
You can also achieve using below code:
extension Optional {
func defaultValue(_ val: Wrapped) -> Wrapped { return self ?? val }
}
var str: String?
str.defaultValue("User")
var a: Int?
a.defaultValue(2)
This will work for both data types.

Swift generic completion handler

I'm trying to figure out how to create a generic completion handler. Below is an example illustrating an example "internal" generic completion handler and the same generic completion handler as I would want to be able to create it if I could do that in "external" form. The problem is that I don't know how to write the equivalent of the internalCompletion<T: MyEnum>... in a completion handler. I've written in the externalCompletion function what I'd imagine it could look like: something along the lines of func externalCompletion(_ completer<T: MyEnum>: ((T) -> Void) where T: Hashable)), but this is obviously not correct. Is what I'm trying to do possible? My hunch is that swift won't let the completion handler remain generic, always requiring a type casting at the function level, which would defeat the purpose per my example (i.e. func externalCompletetion<T: MyEnum>(_ completer: ((T) -> Void)) where T: Hashable, the problem with this being I would have to choose between EnumA, EnumB, and EnumC, not being able to run the completer on all three.)
typealias MyEnumKeyedData<T: MyEnum> = [T: String] where T: Hashable
// MARK : - MyEnum Protocol
protocol MyEnum {
static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable
static var all: [MyEnum] { get }
}
extension MyEnum {
static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable { return Self.all as! [T] }
}
// MARK : - My enums
enum EnumA: MyEnum {
case first
static var all: [MyEnum] { return [EnumA.first]}
}
enum EnumB: MyEnum {
case first
static var all: [MyEnum] { return [EnumB.first]}
}
enum EnumC: MyEnum {
case first
static var all: [MyEnum] { return [EnumC.first]}
}
// MARK : - MyEnum Data Iterator
class MyDataEnumIterator {
var dataA: MyEnumKeyedData<EnumA> = [:]
var dataB: MyEnumKeyedData<EnumB> = [:]
var dataC: MyEnumKeyedData<EnumC> = [:]
func updateData<T: MyEnum>(_ key: T, _ value: String) where T: Hashable {
switch T.self {
case is EnumA.Type: dataA[key as! EnumA] = value
case is EnumB.Type: dataB[key as! EnumB] = value
case is EnumC.Type: dataC[key as! EnumC] = value
default: fatalError("Enum does not exist")
}
}
// Internal (This works)
func internalEnumIterator() {
for key in EnumA.all(EnumA.self) { internalCompletion(key) }
for key in EnumB.all(EnumB.self) { internalCompletion(key) }
for key in EnumC.all(EnumC.self) { internalCompletion(key) }
}
func internalCompletion<T: MyEnum>(_ key: T) where T: Hashable {
let value = "\(key)"
updateData(key, value)
}
// External (This obviously doesn't, just sketching the idea)
func externalEnumIterator(_ completer<T: MyEnum>: ((T) -> Void) where T: Hashable) {
for key in EnumA.all(EnumA.self) { completer(key) }
for key in EnumB.all(EnumB.self) { completer(key) }
for key in EnumC.all(EnumC.self) { completer(key) }
}
}
// MARK : - Test cases (internal works, external does not, just sketching example)
let iterator = MyDataEnumIterator()
iterator.externalEnumIterator({ <T: MyEnum> (T) where T: Hashable in
let value = "\(key)"
iterator.updateData(key, value)
})
iterator.internalEnumIterator()
Here is a working version of the code with the minimal changes necessary to get it going
typealias MyEnumKeyedData<T: MyEnum> = [T: String] where T: Hashable
// MARK : - MyEnum Protocol
protocol MyEnum {
static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable
static var all: [MyEnum] { get }
}
extension MyEnum {
static func all<T: MyEnum>(_:T.Type) -> [T] where T: Hashable { return Self.all as! [T] }
}
// MARK : - My enums
enum EnumA: MyEnum {
case first
static var all: [MyEnum] { return [EnumA.first]}
}
enum EnumB: MyEnum {
case first
static var all: [MyEnum] { return [EnumB.first]}
}
enum EnumC: MyEnum {
case first
static var all: [MyEnum] { return [EnumC.first]}
}
// MARK : - MyEnum Data Iterator
class MyDataEnumIterator {
var dataA: MyEnumKeyedData<EnumA> = [:]
var dataB: MyEnumKeyedData<EnumB> = [:]
var dataC: MyEnumKeyedData<EnumC> = [:]
func updateData(_ key: MyEnum, _ value: String) {
switch key {
case let key as EnumA: dataA[key] = value
case let key as EnumB: dataB[key] = value
case let key as EnumC: dataC[key] = value
default: fatalError("Enum does not exist")
}
}
// Internal (This works)
func internalEnumIterator() {
for key in EnumA.all(EnumA.self) { internalCompletion(key) }
for key in EnumB.all(EnumB.self) { internalCompletion(key) }
for key in EnumC.all(EnumC.self) { internalCompletion(key) }
}
func internalCompletion<T: MyEnum>(_ key: T) where T: Hashable {
let value = "\(key)"
updateData(key, value)
}
func EnumIterator(_ compeltitionHandler: (MyEnum) -> Void) {
for key in EnumA.all(EnumA.self) { compeltitionHandler(key as MyEnum) }
for key in EnumB.all(EnumB.self) { compeltitionHandler(key as MyEnum) }
for key in EnumC.all(EnumC.self) { compeltitionHandler(key as MyEnum) }
}
}
let iterator = MyDataEnumIterator()
iterator.EnumIterator{ key in
let value = "\(key)"
iterator.updateData(key, value)
}
iterator.internalEnumIterator()
Here is a sane version of the code that removes all the nonsense, and adds subscript syntax:
// MARK : - MyEnum Protocol
protocol MyEnum {
static func all() -> [MyEnum]
}
// MARK : - My enums
enum EnumA: MyEnum {
case first
static func all() -> [MyEnum] { return [EnumA.first] }
}
enum EnumB: MyEnum {
case first
static func all() -> [MyEnum] { return [EnumB.first] }
}
enum EnumC: MyEnum {
case first
static func all() -> [MyEnum] { return [EnumC.first] }
}
// MARK : - MyEnum Data Iterator
class MyDataEnumIterator {
var dataA = [EnumA: String]()
var dataB = [EnumB: String]()
var dataC = [EnumC: String]()
subscript(key: MyEnum) -> String? {
get {
switch key {
case let key as EnumA: return dataA[key]
case let key as EnumB: return dataB[key]
case let key as EnumC: return dataC[key]
default: fatalError("Enum does not exist")
}
}
set {
switch key {
case let key as EnumA: dataA[key] = newValue
case let key as EnumB: dataB[key] = newValue
case let key as EnumC: dataC[key] = newValue
default: fatalError("Enum does not exist")
}
}
}
func EnumIterator(_ body: (MyEnum) -> Void) {
EnumA.all().forEach(body);
EnumB.all().forEach(body);
EnumC.all().forEach(body);
}
}
let iterator = MyDataEnumIterator()
iterator.EnumIterator{
iterator[$0] = "\($0)"
}

Can Swift enums be inferred and restricted generically?

I can't find the syntax, but I want to do something like this:
class MyClass {
let stringValue: String // filled in later
let integerValue: Int // filled in later
init(stringValue: String) {
self.stringValue = stringValue
self.integerValue = stringValue.hashValue
}
init(integerValue: Int) {
self.integerValue = integerValue
self.stringValue = String(integerValue)
}
}
extension MyClass {
// This is invalid syntax, but I think you can understand
// vvvvvvvvv I'm trying to give back an enum whose type is inferred
var enumValue<T: enum>: T? {
get {
// This is also invalid; I want to check the type of the enum's raw value
if T is String {
return T(rawValue: self.stringValue)
} else if T is Int {
return T(rawValue: self.integerValue)
} else {
return nil
}
}
}
}
The usage would be like:
enum MyEnum: String {
case foo
case bar
}
func baz(_ some: MyClass) {
if let myEnum: MyEnum = some.enumValue {
print(myEnum)
}
}
let some = MyClass(stringValue: "foo")
baz(some) // prints "foo"
Is this possible in Swift? That is, to have a generically-typed field or function whose type is constricted to enums and inferred based on usage, then use that to instantiate an enum value?
A possible solution would be a generic overloaded function:
extension MyClass {
func enumValue<T: RawRepresentable>() -> T? where T.RawValue == String {
return T(rawValue: stringValue)
}
func enumValue<T: RawRepresentable>() -> T? where T.RawValue == Int {
return T(rawValue: integerValue)
}
}
which is then called as
func baz(_ some: MyClass) {
if let myEnum: MyEnum = some.enumValue() {
print(myEnum)
}
}
Alternatively, pass the enum type as an argument:
extension MyClass {
func enumValue<T: RawRepresentable>(_ type: T.Type) -> T? where T.RawValue == String {
return T(rawValue: stringValue)
}
func enumValue<T: RawRepresentable>(_ type: T.Type) -> T? where T.RawValue == Int {
return T(rawValue: integerValue)
}
}
and call it like
func baz(_ some: MyClass) {
if let myEnum = some.enumValue(MyEnum.self) {
print(myEnum)
}
}