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

Get all methods of an Objective-C class or instance

In addition to Buzzy’s answer, for debugging purposes you may use the -[NSObject _methodDescription] private method. Either in lldb: (lldb) po [[UIApplication sharedApplication] _methodDescription] or in code: @interface NSObject (Private) – (NSString*)_methodDescription; @end // Somewhere in the code: NSLog(@”%@”, [objectToInspect performSelector:@selector(_methodDescription)]); Output will look as following: <__NSArrayM: 0x7f9 ddc4359a0>: in __NSArrayM: Class Methods: + (BOOL) … Read more

Class is implemented in both. One of the two will be used

I wrote that error message!• Either change the class name or don’t link against said library. How is your project configured? Is there anywhere where you explicitly link against SR? Or is it a product of linking against two static libraries that both already include SR? If the former, then stop linking against SR directly … Read more

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

How to implement an IMP function that returns a large struct type determined at run-time?

It seems that the Apple engineer is right: the only to way to go is assembly code. Here are some usefull pointers to getting started: From the Objective-C runtime code: The i386 and x86_64 hand-crafted messenger assmbly stubs for the various messaging methods. An SO answer that provides an overview of the dispatching. A in-depth … Read more

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

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

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 to write iOS app purely in C

Damn, it took me a while but I got it: main.c: #include <CoreFoundation/CoreFoundation.h> #include <objc/runtime.h> #include <objc/message.h> // This is a hack. Because we are writing in C, we cannot out and include // <UIKit/UIKit.h>, as that uses Objective-C constructs. // however, neither can we give the full function declaration, like this: // int UIApplicationMain … Read more