How do I list all fields of an object in Objective-C?

As mentioned, you can use the Objective-C runtime API to retrieve the instance variable names: unsigned int varCount; Ivar *vars = class_copyIvarList([MyClass class], &varCount); for (int i = 0; i < varCount; i++) { Ivar var = vars[i]; const char* name = ivar_getName(var); const char* typeEncoding = ivar_getTypeEncoding(var); // do what you wish with the … Read more

Changed +load method order in Xcode 7

TL,DR: It’s xctest’s fault, not objc’s. This is because of how the xctest executable (the one that actually runs the unit tests, located at $XCODE_DIR/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents/xctest loads its bundle. Pre-Xcode 7, it loaded all referenced test bundles before running any tests. This can be seen (for those that care), by disassembling the binary for Xcode 6.4, … Read more

Swift native base class or NSObject

Swift classes that are subclasses of NSObject: are Objective-C classes themselves use objc_msgSend() for calls to (most of) their methods provide Objective-C runtime metadata for (most of) their method implementations Swift classes that are not subclasses of NSObject: are Objective-C classes, but implement only a handful of methods for NSObject compatibility do not use objc_msgSend() … Read more

How do I lookup a string constant at runtime in Objective-C?

You can use CFBundleGetDataPointerForName to lookup a constant’s value at runtime NSString *lookupStringConstant(NSString *constantName) { void ** dataPtr = CFBundleGetDataPointerForName(CFBundleGetMainBundle(), (__bridge CFStringRef)constantName); return (__bridge NSString *)(dataPtr ? *dataPtr : nil); } Example use: NSString *version = lookupStringConstant(@”VungleSDKVersion”); NSLog(@”Version = %@”,version);

What is objc_setAssociatedObject() and in what cases should it be used?

objc_setAssociatedObject adds a key value store to each Objective-C object. It lets you store additional state for the object, not reflected in its instance variables. It’s really convenient when you want to store things belonging to an object outside of the main implementation. One of the main use cases is in categories where you cannot … Read more

Get property name as a string

You can try this: unsigned int propertyCount = 0; objc_property_t * properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray * propertyNames = [NSMutableArray array]; for (unsigned int i = 0; i < propertyCount; ++i) { objc_property_t property = properties[i]; const char * name = property_getName(property); [propertyNames addObject:[NSString stringWithUTF8String:name]]; } free(properties); NSLog(@”Names: %@”, propertyNames);

What exactly is super in Objective-C?

super Essentially, it allows you to use the implementations of the current class’ superclass. For the gritty details of the Objective-C runtime: [super message] has the following meaning: When it encounters a method call, the compiler generates a call to one of the functions objc_msgSend, objc_msgSend_stret, objc_msgSendSuper, or objc_msgSendSuper_stret. Messages sent to an object’s superclass … Read more

Why doesn’t Objective-C support private methods?

The answer is… well… simple. Simplicity and consistency, in fact. Objective-C is purely dynamic at the moment of method dispatch. In particular, every method dispatch goes through the exact same dynamic method resolution point as every other method dispatch. At runtime, every method implementation has the exact same exposure and all of the APIs provided … Read more

How can I add properties to an object at runtime?

It’s possible to add formal properties to a class via class_addProperty(): BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount) The first two parameters are self-explanatory. The third parameter is an array of property attributes, and each property attribute is a name-value pair which follow Objective-C type encodings for declared properties. Note … Read more