Untitled
unknown
plain_text
6 days ago
1.2 kB
2
Indexable
Never
import Foundation import ObjectiveC.runtime // Function to get a private property from an object func getPrivateProperty(from object: NSObject, propertyName: String) -> AnyObject? { // 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) as AnyObject? } } free(properties) } return nil } // Example usage let myObject = MyClass() // Retrieve the private property if let privateValue = getPrivateProperty(from: myObject, propertyName: "hiddenProperty") as? HiddenProtocol { print("Accessed private property successfully") privateValue.hiddenMethod() // Call the method in the protocol } else { print("Failed to access private property") }
Leave a Comment