Last active
September 14, 2020 11:17
-
-
Save raygun101/38583876373ef1401f8a14bdd2f61f3e to your computer and use it in GitHub Desktop.
π° Layered Cakewalk [Swift] - Get All Types at Runtime (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
/// | |
/// π° Layered Cakewalk | |
/// | |
/// Gets all defined `Swift` and `Objective-C` classes. | |
/// | |
public static func allTypes() -> [AnyObject.Type] | |
{ | |
let typeCount = Int(objc_getClassList(nil, 0)) | |
let types = UnsafeMutablePointer<AnyObject.Type>.allocate(capacity: typeCount) | |
defer { types.deallocate() } | |
let safeTypes = AutoreleasingUnsafeMutablePointer<AnyObject.Type>(types) | |
objc_getClassList(safeTypes, Int32(typeCount)) | |
return [AnyObject.Type](unsafeUninitializedCapacity: typeCount) | |
{ | |
buffer, initializedCapacity in | |
for index in 0 ..< typeCount | |
{ | |
buffer[index] = safeTypes[index] | |
} | |
initializedCapacity = typeCount | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment