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

How are NSBlock objects created?

I will preface this question by stating that what I am about to ask is for educational and possibly debug purposes only. How are block objects created internally in the Objective C runtime? I see the hierarchy of classes that all represent various…
Léo Natan
  • 56,823
  • 9
  • 150
  • 195
15
votes
2 answers

Obtain list of class methods for an arbitrary class

How can I get the list of class methods for a particular Class? I've tried using the class_copyMethodList function declared in , but that's only giving me instance methods. I've also found a function that gives me a Method for a…
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
14
votes
3 answers

Objective-C: preferred way to retrieve the superclass of a Class instance

I am wondering which of the two following methods is the correct or preferred one to retrieve the superclass of a Class variable: Class getSuperclass(Class cls) { return [cls superclass]; } Class getSuperclass(Class cls) { return…
MKroehnert
  • 3,637
  • 2
  • 34
  • 43
14
votes
3 answers

How are the digits in ObjC method type encoding calculated?

Is is a follow-up to my previous question: What are the digits in an ObjC method type encoding string? Say there is an encoding: v24@0:4:8@12B16@20 How are those numbers calculated? B is a char so it should occupy just 1 byte (not 4 bytes). Does it…
Ecir Hana
  • 10,864
  • 13
  • 67
  • 117
13
votes
1 answer

Lazily named class wasn't named by lazy name handler

The warning below gets occasionally reported in the Xcode console. What does this warning mean and how do I stop it from happening? objc[4082]: Lazily named class 0x7ffee3d07ba0 wasn't named by lazy name handler
Adam Dunmars
  • 361
  • 1
  • 11
13
votes
1 answer

Objective-C macro to detect if variable is primitive

I'm looking a macro to detect if a variable is an object or a primitive in Objective-C. In this context, I know the parameter must be a variable and will never be an expression. Here is the best thing I've come up with: #define IS_OBJECT(x) (…
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
13
votes
2 answers

object_getInstanceVariable works for float, int, bool, but not for double?

I've got object_getInstanceVariable to work as here however it seems to only work for floats, bools and ints not doubles. I do suspect I'm doing something wrong but I've been going in circles with this. float myFloatValue; float someFloat =…
Russel West
  • 205
  • 3
  • 7
13
votes
1 answer

Why can't we use C-strings as SELs?

So, I've been messing around with the objc-runtime again (surprise surprise), and I found an interesting block of code here: const char *sel_getName(SEL sel) { #if SUPPORT_IGNORED_SELECTOR_CONSTANT if ((uintptr_t)sel == kIgnore) return "
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
12
votes
4 answers

Objective-C Reflection for generic NSCoding implementation

Is there any means of reflection in Objective-C that would allow you to write generic NSCoding implementations by inspecting the public properties of an object and generating generic implementations of encodeWithCoder: and initWithCoder: . I'm…
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
12
votes
4 answers

Method cannot be marked @objc because its result type cannot be represented in Objective-C

am exposing swift API's in Objective-C and the Objective-C runtime. When i add "@objc" before the function throws an error "Method cannot be marked @objc because its result type cannot be represented in Objective-C" My code is here @objc public…
SriMa
  • 197
  • 1
  • 1
  • 12
12
votes
1 answer

Is there any way to list all swizzled methods in an iOS app?

I'm essentially looking for a way to detect when/what third party libraries swizzle. I recently ran into a situation where an ad library used an oddball fork of AFNetworking. AFNetworking swizzles NSURLSessionTask, and the two swizzles didn't play…
BTK
  • 123
  • 5
12
votes
2 answers

object_setClass to bigger class

I am changing the class of some objects using object_setClass(id object, Class cls). I am changing the class to a subclass of the original class. Then I set some properties that are only defined on the subclass, and things seem to work fine. I was a…
Felixyz
  • 19,053
  • 14
  • 65
  • 60
12
votes
2 answers

Swift isa pointer remapping or other supported method swizzling

Do Swift classes have something like an isa pointer that can be remapped? We've seen that Swift uses a more static method dispatch than objective-C, which (unless a class dervices from Foundation/NSObject) prevents the style of swizzling based on…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
12
votes
1 answer

Why do protocol_* methods do not work with Clang + modern GCC-Runtime on Linux?

I tried to switch some of my Objective-C projects from GCC to Clang on Linux. I used the GCC 4.6.2 runtime because the Clang compiler does not ship with one. The compiling and linking works, but when using the protocol_* methods they do not…
Tilo Prütz
  • 1,766
  • 3
  • 16
  • 27
11
votes
1 answer

How can one obtain the sizeof a type for which one has an encoding?

Given an Objective-C type type, one can obtain the encoding encoding and size size of the type easily: const char *encoding = @encode(type); size_t size = sizeof(type); Put a little differently, we have mappings @encode: type_t -> const char…
Nate Chandler
  • 4,533
  • 1
  • 23
  • 32
1 2
3
41 42