Skip to content

Instantly share code, notes, and snippets.

@raygun101
Last active September 14, 2020 11:17
Show Gist options
  • Save raygun101/38583876373ef1401f8a14bdd2f61f3e to your computer and use it in GitHub Desktop.
Save raygun101/38583876373ef1401f8a14bdd2f61f3e to your computer and use it in GitHub Desktop.
🍰 Layered Cakewalk [Swift] - Get All Types at Runtime (Swift)
///
/// 🍰 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