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
7
votes
3 answers

Obj-c protocol properties are not implemented in conforming class

Problem I've come across an interesting issue and wasn't able to find any documentation on it... Sometimes properties declared in a protocol are not implemented in a particular class conforming to that protocol and a runtime exception occurs. Are…
Christopher
  • 193
  • 7
7
votes
3 answers

Can I create an Objective-C class at run time from a text file?

I want to create an Objective C class at runtime from a file. So for example I have an objective c application, I want to then point it at a text file (or an .h .m pair, whatever works) and then have the application parse the text file and create…
Samuel Rosen
  • 183
  • 1
  • 2
  • 5
7
votes
2 answers

Create custom dynamic classes in objective-c

In my application i have an UIViewController that i uses a lot of UIAlertView to ask things to the user. Because i need the response of each UIAlertView i have made my controller a delegate of UIAlertViewDelegate, this works fine but after 7…
Nicos Karalis
  • 3,724
  • 4
  • 33
  • 62
7
votes
2 answers

Call instance method with objc_msgSend

I'm trying to use the objc_msgSend method to call some method dynamically. Say I want call some method in Class B from Class A and there are two methods in class B like: - (void) instanceTestWithStr1:(NSString *)str1 str2:(NSString *)str1; + (void)…
supersuraccoon
  • 1,621
  • 4
  • 20
  • 36
7
votes
5 answers

How do I get the int value from object_getIvar(self, myIntVar) as it returns a pointer

if the variable in object_getIvar is a basic data type (eg. float, int, bool) how do I get the value as the function returns a pointer (id) according to the documentation. I've tried casting to an int, int* but when I try to get that to NSLog, I…
7
votes
1 answer

What is an Objective-C 2.0 class interface and implementation converted into by GCC or Clang

Since Objective-C is a C superset, all Objective-C specific statements are converted into C statements during the compiling of a .m file (by the preprocessor, I guess). So, for example, a message expression like [receiver method] is converted into a…
LuisABOL
  • 2,951
  • 4
  • 25
  • 57
7
votes
2 answers

why does this code give EXC_BAD_ACCESS (using IMP)

This code gives me EXC_BAD_ACCESS, why? NSMutableDictionary *d = [[NSMutableDictionary alloc] init]; IMP imp= [d methodForSelector:@selector(setObject:forKey:) ]; imp(d, @selector( setObject:forKey:), @"obj", @"key"); I'm just starting using IMP,…
subzero
  • 3,420
  • 5
  • 31
  • 40
7
votes
2 answers

Change enum values at runtime?

Is there a way to assign values to enums during runtime in objective c? I have several enums and want each of the enum to have certain value. The values could be read from a xml file. Is there a way to do this?
i_raqz
  • 2,919
  • 10
  • 51
  • 87
6
votes
1 answer

Objective-C Runtime: Swizzled method name?

In an attempt to Detect backspace in UITextField, I've tried subclassing UITextField and overriding -[UIKeyInput deleteBackward], but it never gets called. So, I'm suspecting UITextField swizzles deleteBackward to another method name. Using the…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
6
votes
3 answers

Using Objective-C Metadata to Generate Class Dependency Graph

This guy came up with a pretty neat tool to generate a class dependency graph - however, it relies on parsing your source code and looking for #import directives.…
Steve
  • 31,144
  • 19
  • 99
  • 122
6
votes
1 answer

Example of how Objective-C's @try-@catch implementation is executed at runtime?

In Objective-C's low-level runtime headers (/usr/include/objc), there is an objc-exceptions.h file. It would seem this is how @try/@catch is implemented by the ObjC compiler. I am trying to invoke these functions manually (for experimentations with…
TooTallNate
  • 1,477
  • 3
  • 20
  • 41
6
votes
1 answer

argument isKindOfClass: [NSNumber class] - sane way to check this?

So I was playing with something where the class type of the arg is unknown until runtime. like this: - (NSNumber *)doWhatever:(id)arg { // this ALWAYS FAILS if ([arg isKindOfClass:[NSNumber class]]) { return arg; } else { …
jpswain
  • 14,642
  • 8
  • 58
  • 63
6
votes
1 answer

What determines the process by which unimplemented methods are resolved?

As I understand it, an unimplemented method gets resolved in the following way: resolveInstanceMethod: / resolveClassMethod: gets a chance to implement the method forwardingTargetForSelector: gets a chance to forward to a…
Chris Devereux
  • 5,453
  • 1
  • 26
  • 32
6
votes
2 answers

Xcode -- finding dead methods in a project

I am curious if there are any tools that provide partial solutions for this. It is a tricky problem because of performSelector . . . but a tool ought to at least be able to come up with candidates, making the human's job easier.
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
6
votes
3 answers

Is Objective C really compiled ? Isn't it more like Visual Basic / .NET runtime ? Then what prevents it to be portable to other platforms?

Syntactically it's superset of C. But since messages are sent and processed at runtime this means it cannot be a pure compiled language like c but it needs a runtime like Visual Basic or .Net runtime. Then what prevents it to be portable to other…
user310291
  • 36,946
  • 82
  • 271
  • 487