Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. ncreated created this gist Mar 2, 2023.
    22 changes: 22 additions & 0 deletions NSObject+allMethods+allProperties.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    extension NSObject {
    var __methods: [Selector] {
    var methodCount: UInt32 = 0
    guard
    let methodList = class_copyMethodList(type(of: self), &methodCount),
    methodCount != 0
    else { return [] }
    return (0 ..< Int(methodCount))
    .compactMap({ method_getName(methodList[$0]) })
    }

    var __properties: Any {
    var propertiesCount: UInt32 = 0
    guard
    let propertiesList = class_copyPropertyList(type(of: self), &propertiesCount),
    propertiesCount != 0
    else { return [] }
    return (0 ..< Int(propertiesCount))
    .compactMap({ property_getName(propertiesList[$0]) })
    .map({ NSString(cString: $0, encoding: NSUTF8StringEncoding) })
    }
    }