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
25
votes
6 answers

Using objc_setAssociatedObject with weak references

I know that OBJC_ASSOCIATION_ASSIGN exists, but does it zero the reference if the target object is dealloced? Or is it like the old days where that reference needs to get nil-ed or we risk a bad access later on?
ultramiraculous
  • 1,062
  • 14
  • 21
24
votes
3 answers

Why shouldn't you use objc_msgSend() in Objective C?

The Objective C Runtime Guide from Apple, states that you should never use objc_msgSend() in your own code, and recommends using methodForSelector: instead. However, it doesn't provide any reason for this. What are the dangers of calling…
cfischer
  • 24,452
  • 37
  • 131
  • 214
24
votes
3 answers

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

If I have a class, how can I list all its instance variable names? eg: @interface MyClass : NSObject { int myInt; NSString* myString; NSMutableArray* myArray; } I would like to get "myInt", "myString", and "myArray". Is there some way…
Paul Green
23
votes
3 answers

Avoid extra static variables for associated objects keys

When using associated objects, an Objective-C runtime feature available starting from iOS 4 and OSX 10.6, it's necessary to define a key for storing and retrieving the object at runtime. The typical usage is defining the key like follows static char…
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
20
votes
3 answers

What is the underlying mechanism for ivar synthesis in the modern Objective C runtime

One of the features of the modern (64 bit OS X and iPhone OS) Objective C runtime is the ability for properties to dynamically synthesize ivars without explicitly declaring them in the class: @interface MyClass : NSObject { // NSString *name;…
Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
20
votes
1 answer

NSProxy vs NSObject

I was using method swizzling to wrap all method invocations in a class with some extra functionality. Specifically I was: Checking if the required object for this method call was in the cache If the cache had that object return it. If not,…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
20
votes
2 answers

Creating an IMP from an Objective-C block

The IMP type in Objective-C represents a function pointer, as far I as understand. Is there any way to make an IMP from a block pointer? Thanks for your ideas.
19
votes
1 answer

Error when using "setValue: forKey:" on an NSString's isa pointer then calling [string class]

Heres's what I've got as to error. libobjc.A.dylib`_objc_trap(): 0x14c13f4: pushl %ebp 0x14c13f5: movl %esp, %ebp 0x14c13f7: ud2 So basically I'm trying to understand How NSString works and trying to find a way to change the pointer that…
denis_choe
  • 667
  • 6
  • 15
19
votes
3 answers

How to implement an NSRunLoop inside an NSOperation

Im posting this question because I have seen a lot of confusion over this topic and I spent several hours debugging NSOperation subclasses as a result. The problem is that NSOperation doesnt do you much good when you execute Asynchronous methods…
deleted_user
  • 3,817
  • 1
  • 18
  • 27
17
votes
2 answers

How to dynamically add a class method?

Using the Objective-C Runtime, how do I add the method +layerClass to the private UIGroupTableViewCellBackground class (not to its superclass, UIView)? Note: This is only for testing (to see how UITableViewStyleGrouped sets cell backgroundView &…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
17
votes
1 answer

Using objc_getClassList under arc

Has anybody managed to use objc_getClassList under arc, short of turning arc off for the file in question? The fundamental problem is that one of the parameters is a C array of Class pointers.
William Jockusch
  • 26,513
  • 49
  • 182
  • 323
17
votes
3 answers

Why do Objective-C objects have to be dynamically allocated?

Why do Objective-c objects have to be dynamically allocated? Why do I have to make it a pointer to an object, unlike in C++ I can create them on stack? Thanks.
17
votes
2 answers

Changed +load method order in Xcode 7

I found out that Xcode 7 (Version 7.0 (7A220)) changed the order in which +load methods for classes and categories are called during unit tests. If a category belonging to the test target implements a +load method, it is now called at the end, when…
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
17
votes
1 answer

What are the digits in an ObjC method type encoding string?

I'm reading Apple's article about Objective-C runtime type encoding strings and some methods have numbers in their type strings. What do the numbers in v12@0:4@8 mean?
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
15
votes
1 answer

Why can some methods (-retainWeakReference, -allowsWeakReference, +load, +initialize) on class NSObject not be added to other classes at runtime?

It is straightforward at runtime to create a copy MyNSObject of the Class NSObject: First, create a new class pair. Class MyNSObject = objc_allocateClassPair(nil, "MyNSObject", 0); Second read the methods, protocols, and ivars from NSObject and add…
Nate Chandler
  • 4,533
  • 1
  • 23
  • 32
1
2
3
41 42