Untitled
unknown
plain_text
5 days ago
1.0 kB
2
Indexable
Never
import Foundation import ObjectiveC.runtime // Function to get private properties from an NSObject func getPrivateProperty(from object: NSObject, propertyName: String) -> Any? { // Get the class of the object let objectClass: AnyClass = object_getClass(object)! // Get the list of properties of the class var count: UInt32 = 0 if let properties = class_copyPropertyList(objectClass, &count) { for i in 0..<Int(count) { let property = properties[i] let name = String(cString: property_getName(property)) if name == propertyName { // Access the private property using KVC return object.value(forKey: name) } } free(properties) } return nil } // Usage let myObject = MyClass() if let privateValue = getPrivateProperty(from: myObject, propertyName: "privateProperty") as? String { print("Private property value: \(privateValue)") } else { print("Failed to access private property") }
Leave a Comment