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
9
votes
2 answers

Storing things in isa

The 64-bit runtime took away the ability to directly access the isa field of an object, something CLANG engineers had been warning us about for a while. They've been replaced by a rather inventive (and magic) set of everchanging ABI rules about…
CodaFi
  • 43,043
  • 8
  • 107
  • 153
9
votes
3 answers

Can Foundation tell me whether an Objective-C method requires a special structure return?

Background as I understand it: Objective-C method invocations are basically a C function call with two hidden parameters (the receiver and the selector). The Objective-C runtime contains a function named objc_msgSend() that allows to invoke methods…
Erik Doernenburg
  • 2,933
  • 18
  • 21
9
votes
2 answers

How can I create a block that 'wraps' a target/selector pair?

I love blocks, and they are very cool. However, I find that blocks can clutter up my code and make it harder to read without folding all of them up inside Xcode (which I don't like doing). I like splitting my code into logical methods (selectors)…
9
votes
5 answers

Using instance variables with Modern Runtime

I have several years of experience in Obj-c and Cocoa, but am just now getting back into it and the advances of Obj-C 2.0 etc. I'm trying to get my head around the modern runtime and declaring properties, etc. One thing that confuses me a bit is the…
Scott Little
  • 1,929
  • 15
  • 16
8
votes
2 answers

How do I return a struct value from a runtime-defined class method under ARC?

I have a class method returning a CGSize and I'd like to call it via the Objective-C runtime functions because I'm given the class and method names as string values. I'm compiling with ARC flags in XCode 4.2. Method…
8
votes
3 answers

Can class_addMethod in Objective-C work only on a specific instance?

I am trying to write some dynamic code where a user can try calling a method from a specific instance of a class and have it be resolved at runtime. The implementation to retrieve the information exists but the method to access it does not because…
gtaborga
  • 773
  • 3
  • 13
  • 18
8
votes
1 answer

Associated objects in Swift, does global key actually produce specific instances?

To have an associated object in Swift, you simply use a memory address as a handle, and then use the objc calls. The usual example code you can google everywhere is: var keyA:UInt8 = 0 var keyB:UInt8 = 0 extension UIViewController { var aoAA:…
Fattie
  • 27,874
  • 70
  • 431
  • 719
8
votes
2 answers

What is the difference between declaring a protocol @objc and having it conform to NSObjectProtocol in pure Swift?

Consider two Swift protocols: @objc protocol SomeProtocol { } protocol SomeOtherProtocol: NSObjectProtocol { } What is the difference between declaring a Swift protocol @objc or having it conform to NSObjectProtocol? I know that any protocol that…
JAL
  • 41,701
  • 23
  • 172
  • 300
8
votes
1 answer

Why is isa field of Objective C object has 1 offset with its class pointer?

I'm trying to using the following code to understand NSObject's C struct layout. First I print its struct layout using NSData, then I print the class pointer directly using [NSObject class]. id obj = [NSObject new]; long nsSize =…
Zhu Shengqi
  • 3,632
  • 3
  • 24
  • 29
8
votes
3 answers

Objective-C Address of property expression

I need access address of property but have problem. example code is @interface Rectangle : NSObject { SDL_Rect wall; SDL_Rect ground; } @property SDL_Rect wall; @property SDL_Rect ground; @end @implementation Rectangle @synthesize…
unknow
  • 869
  • 1
  • 7
  • 10
8
votes
1 answer

Why can't gcc or clang properly @encode SIMD vector types?

While doing some messing around with vector types and the ObjC runtime, I came across a very perplexing problem. Neither clang or GCC will give the 'proper' type-encoding for any SIMD vector type, as far as I can tell. #import…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
8
votes
2 answers

Objective-C runtime: What does declaring a variable of type Class (objc_class) conforming to a protocol mean?

Class bah = [NSString class]; id object = [bah new]; Compiles with absolutely no issues. Class bah = [NSString class]; id object = [bah new]; Returns the error "No known class method for selector 'new'". Why does the first…
Reid Main
  • 3,394
  • 3
  • 25
  • 42
8
votes
1 answer

In Objective-C, when are metaclass object and class object created?

In Objective-C, when are the class pairs - metaclass object and class object, created? Is it in the beginning of the app execution or at the point when you first instantiate the first object of a class?
Boon
  • 40,656
  • 60
  • 209
  • 315
8
votes
1 answer

How does objc_setAssociatedObject work?

As we know, we can add a variable in Objective-C using a category and runtime methods like objc_setAssociatedObject and objc_getAssociatedObject. For example: #import @interface Person (EmailAddress) @property (nonatomic,…
foogry
  • 835
  • 7
  • 17
8
votes
3 answers

Is there any introspection method to get all adopted protocols for a class in Objective-C?

There is an -[NSObject conformsToProtocol:] method to check whether a specific protocol is adopted or not. Is there any method to get all adopted protocols for a class, rather than checking a list?