I am trying to replicate this functionality in swift: https://stackoverflow.com/a/11774276/4102858. I was able to successfully recreate allPropertyNames() in Swift. However, I'm having trouble doing the same with the method pointerOfIvarForPropertyNamed(). The main reason is that the Objc solution uses the function object_getInstanceVariable(), which isn't available in Swift.
TLDL: Im trying to recreate this method in Swift:
- (void *)pointerOfIvarForPropertyNamed:(NSString *)name
{
objc_property_t property = class_getProperty([self class], [name UTF8String]);
const char *attr = property_getAttributes(property);
const char *ivarName = strchr(attr, 'V') + 1;
Ivar ivar = object_getInstanceVariable(self, ivarName, NULL);
return (char *)self + ivar_getOffset(ivar);
}
This is where I'm at now:
func pointerOfIvarForPropertyNamed(name: String) -> AnyObject {
func unicodeValueOfString(string: String) -> UInt32 {
for s in String(string).unicodeScalars {
return s.value
}
return 0
}
var property: COpaquePointer = class_getProperty(self.classForCoder, (name as NSString).UTF8String)
var attr: UnsafePointer<Int8> = property_getAttributes(property)
var ivarName: UnsafeMutablePointer<Int8> = strchr(attributesPointer, Int32(unicodeValueOfString("V"))) + 1
//object_getInstanceVariable() not available in Swift
}
Problems:
The function object_getInstanceVariable() is unavailable in Swift
My general lack of experience using Objective-C runtime