Untitled
unknown
plain_text
a year ago
749 B
9
Indexable
import Foundation
class MyClass {
private var privateProperty: String = "Secret"
}
// Helper function to get a private property using unsafe APIs
func getPrivateProperty<T>(from object: AnyObject, propertyName: String, type: T.Type) -> T? {
let mirror = Mirror(reflecting: object)
// Search for the property
for child in mirror.children {
if let label = child.label, label == propertyName {
return child.value as? T
}
}
return nil
}
let myObject = MyClass()
if let privateValue: String = getPrivateProperty(from: myObject, propertyName: "privateProperty", type: String.self) {
print("Private property value: \(privateValue)")
} else {
print("Failed to access private property")
}
Editor is loading...
Leave a Comment