Questions tagged [objective-c-runtime]

The Objective-C runtime is a runtime support library provided with an implementation of the Objective-C language. Its API allows dynamically creating and configuring classes at runtime, as well as introspecting existing classes, methods, properties, and method implementations.

Use this tag for questions that involve interacting with the Objective-C runtime. This includes fundamental methods of NSObject, <NSObject>, and NSProxy, as well as the API exposed by the files in <objc/> such as <objc/objc-runtime.h>.

Very few Objective-C development tasks require interacting with the runtime:

  • Introspecting the runtime environment, for example to produce a live class browser or graph the relationships between classes or objects at runtime.
  • Creating a proxy object to interact with the message forwarding machinery.
  • Dynamically creating classes and methods at runtime, for example, to support a scripting interface.
  • Optimization hacks for repeated message sends, such as hoisting the method lookup out of the loop and calling the implementing function directly.
  • Writing an application without using objective-c, such as C or C++

Many of these topics have been thoroughly addressed in blog form by Mike Ash in his Friday Q&A series of blog posts.

Related tags that might be more appropriate for your question:

  • Use for questions about the language rather than its runtime, including problems with reference counting.
  • Use or for problems with the Xcode IDE.
  • Use or for problems with these collections of Objective-C APIs.
626 questions
6
votes
2 answers

Different behavior on iPhone Emulator and Real Device about Message Forwarding

I want to use Message Forwarding to let any unimplemented getter method return 0, instead of throw a unrecognized selector exception. Like MyClass *r = [[MyClass alloc] init]; NSNumber *n = (NSNumber *)r; NSLog(@"%d", [n integerValue]); // output…
mmin
  • 221
  • 2
  • 4
6
votes
3 answers

How to call an implementation with variable number of arguments?

To simplify, let's say I have a function like that void myFunc(id _self, SEL _cmd, id first, ...) { } In that method I wanna call the implementation(imp) on the superclass of _self. I can reach that IMP with this code: Class class =…
iSofTom
  • 1,718
  • 11
  • 15
6
votes
2 answers

Calling Objective-C method from imp

I'm trying to get a Method at runtime and then use its data structure to call it's implementation. Just to clarify, this is for learning purposes, not for any practical reason. So here is my code. #import #import…
5
votes
2 answers

Copy a method IMP for multiple method swizzles

I have a class set up that ideally will read the methods of any class passed in and then map all of them to on single selector at runtime before forwarding them off to their original selector. This does work right now, but I can only do it for one…
Jack Freeman
  • 1,414
  • 11
  • 18
5
votes
1 answer

What's the format for `objc_method_description.types` for swift blocks?

The Objective-C method encoding produced by the Swift compiler for swift methods with blocks seems to use syntax not documented anywhere. For instance the method: func DebugLog2(message: String) async -> String has the method…
5
votes
3 answers

When using Objection, implementation of custom protocol crashes with unrecognized selector

I am defining a custom protocol: @protocol NGSAuthProvider - (BOOL)isReady; - (BOOL)isSessionValid; - (void)login; - (void)logout; - (NSString *)accessToken; - (BOOL)handleOpenURL:(NSURL *)url; @end I want to have different providers. …
chrish
  • 2,352
  • 1
  • 17
  • 32
5
votes
1 answer

adding methods dynamically

I am trying this method found in Obj-c runtime reference BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) I want to add a new method like: - [AClass drawWithFrame:(NSRect)rect inView:(id)view] So far I have written a C…
nacho4d
  • 43,720
  • 45
  • 157
  • 240
5
votes
2 answers

Cocoa/Objective-C Plugins Collisions

My application has a plugin system that allows my users to write their own plugins that get loaded at runtime. Usually this is fine but in some cases two plugins use the same libraries that will cause a collision between those two. Example: Plugin A…
hjaltij
  • 885
  • 6
  • 8
5
votes
1 answer

Using objc_disposeClassPair()

There's an undocumented function in the runtime API, which appears (on the basis of a few toy programs) to do what its name suggests it does: OBJC_EXPORT void objc_disposeClassPair(Class cls) …
Chris Devereux
  • 5,453
  • 1
  • 26
  • 32
5
votes
1 answer

Swift runtime - calling superclass method

I'm creating a subclass of UIView in runtime and providing my implementation of the layoutSubviews method for it. One imporant thing that I need to do is to perform super.layoutSubviews(). In Objective-C I can do it using the objc_msgSendSuper…
Ruslan Serebriakov
  • 640
  • 1
  • 4
  • 12
5
votes
1 answer

Why does Apple's implementation of KVO use subclassing instead of swizzling?

In Apple's Key-Value Observing Implementation Details document, it says the implementation creates a subclass to forward setter methods. The subclass replaces the original class. Why not just use method_exchangeImplementations() to swizzle the…
5
votes
2 answers

Swift 4 Objective-C Runtime and casting to NSObjectProtocol

In Swift 3, I had a snipped of code that called into Objective-C runtime to check if a certain class is present. guard let managerClass = NSClassFromString("ASIdentifierManager") as? NSObjectProtocol else { return nil } This returns AnyClass…
Legoless
  • 10,942
  • 7
  • 48
  • 68
5
votes
4 answers

What's required to implement root class of Objective-C?

I tried this code: // main.m #import @interface Test + (void)test; @end @implementation Test + (void)test { printf("test"); } @end int main() { [Test test]; return 0; } with LLVM/Clang without any framework, it doesn't…
eonil
  • 83,476
  • 81
  • 317
  • 516
5
votes
1 answer

Why resolveInstanceMethod: called twice sometimes

Recently, I'm studying the runtime in Objective-C. I created a class named TO: @interface TO : NSObject @end #import "TO.h" @implementation TO - (id)forwardingTargetForSelector:(SEL)aSelector { NSLog(@"%@ sel: %@", NSStringFromSelector(_cmd),…
Veight Zhou
  • 969
  • 1
  • 7
  • 8
5
votes
1 answer

Swift 2.0 replicate OBJC_ASSOCIATION_RETAIN

I'm extending some classes in Swift 2.0 to work with ReactiveCocoa 3.0 (swift-2.0 branch), but have run into some trouble. I've followed Colin Eberhardt's tutorial, and have copy pasted some of his UIKit extension logic over to my OS X app. It all…
Chris
  • 7,830
  • 6
  • 38
  • 72