Skip to content

Instantly share code, notes, and snippets.

@usagimaru
Last active November 7, 2025 17:46
Show Gist options
  • Select an option

  • Save usagimaru/76852d6102413c05b549f91a522e60cd to your computer and use it in GitHub Desktop.

Select an option

Save usagimaru/76852d6102413c05b549f91a522e60cd to your computer and use it in GitHub Desktop.
Copy instance variables, methods or properties from specific class with Objective-C runtime API via Swift.
#import ObjectiveC
var count: UInt32 = 0
if let ivars = class_copyIvarList(NSObject.self, &count) {
for i in 0..<Int(count) {
let ivar = ivars[i]
if let name = ivar_getName(ivar) {
print("ivar: ", String(describing: String(utf8String: name)))
}
}
}
#import ObjectiveC
var count: UInt32 = 0
if let methods = class_copyMethodList(NSObject.self, &count) {
for i in 0..<Int(count) {
let method = methods[i]
let sel = method_getName(method)
let name = sel_getName(sel)
print("method: ", String(describing: String(utf8String: name)))
}
}
#import ObjectiveC
var count: UInt32 = 0
if let props = class_copyPropertyList(NSObject.self, & count) {
for i in 0..<Int(count) {
let prop = props[i]
let name = property_getName(prop)
print("property: ", String(describing: String(utf8String: name)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment