Skip to content

Instantly share code, notes, and snippets.

@brianmichel
Created December 15, 2016 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmichel/031397fc9c7d74b61fdddb75bfea0e9a to your computer and use it in GitHub Desktop.
Save brianmichel/031397fc9c7d74b61fdddb75bfea0e9a to your computer and use it in GitHub Desktop.
Returned value is a string, but cannot be cast to as String
import Foundation
protocol KeyValueStorage {
func set(_ value: Any?, forKey key: String)
func value(forKey key: String) -> Any?
}
final class DictionaryKeyValueWrapper: KeyValueStorage {
var dictionary = [String: Any?]()
func set(_ value: Any?, forKey key: String) {
dictionary[key] = value
}
func value(forKey key: String) -> Any? {
return dictionary[key] as Any
}
}
let storage = DictionaryKeyValueWrapper()
storage.set("1", forKey: "awesome")
let returned = storage.value(forKey: "awesome")
let string = storage.value(forKey: "awesome") as? String
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment