Last active
September 24, 2020 06:36
-
-
Save raygun101/61df142e648772340731029db51f539e to your computer and use it in GitHub Desktop.
π° Layered Cakewalk [Swift] - Generic<> stored static fields workaround. (Swift)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// π°π» Generic<> stored static fields workaround. | |
// | |
private var _MyClass_static: [ObjectIdentifier : Any] = [:] // need some global space | |
// | |
class MyClass<T> | |
{ | |
typealias StaticClass = MyClassStatic<Self> | |
static var `static`: StaticClass | |
{ | |
let key = ObjectIdentifier(Self.self) | |
return | |
(_MyClass_static[key] as! StaticClass?) | |
?? | |
{ | |
let result = StaticClass() | |
_MyClass_static[key] = result | |
return result | |
}() | |
} | |
} | |
class MyClassStatic<TMyClass> | |
{ | |
var staticValue: String = "" | |
} | |
MyClass<String>.`static`.staticValue = "wolly" | |
MyClass<Int>.`static`.staticValue = "fred" | |
print(MyClass<String>.`static`.staticValue) // wolly | |
print(MyClass<Int>.`static`.staticValue) // fred |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π°β± Usage Example
Copy into a playground, and play...