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
374
votes
4 answers

How to write iOS app purely in C

I read here Learn C Before Objective-C? Usually I then replace some Obj-C code with pure C code (after all you can mix them as much as you like, the content of an Obj-C method can be entirely, pure C code) Is this true? Is it possible to build an…
001
  • 62,807
  • 94
  • 230
  • 350
131
votes
3 answers

Objective-C class -> string like: [NSArray className] -> @"NSArray"

I am trying to get a string name of a class from the class object itself. // For instance [NSArray className]; // @"NSArray" I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless…
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
124
votes
10 answers

Why doesn't Objective-C support private methods?

I've seen a number of strategies for declaring semi-private methods in Objective-C, but there does not seem to be a way to make a truly private method. I accept that. But, why is this so? Every explanation I've essentially says, "you can't do it,…
Rob Jones
  • 4,925
  • 3
  • 32
  • 39
109
votes
6 answers

Swift native base class or NSObject

I tested out some isa swizzling with Swift, and found that it only works when NSObject is a super-class (directly or further up), or by using the '@objc' decoration. Otherwise it will follow a static- and vtable-dispatch style, like C++. Is it…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
76
votes
4 answers

What is objc_setAssociatedObject() and in what cases should it be used?

In a project I have taken on, the original author has opted to use objc_setAssociatedObject() and I'm not 100% clear what it does or why they decided to use it. I decided to look it up and, unfortunately, the docs aren't very descriptive about its…
Jasarien
  • 58,279
  • 31
  • 157
  • 188
59
votes
1 answer

How to implement an IMP function that returns a large struct type determined at run-time?

Background: CamelBones registers Perl classes with the Objective-C runtime. To do this, every Perl method is registered with the same IMP function; that function examines its self & _cmd arguments to find which Perl method to call. This has worked…
Sherm Pendley
  • 13,556
  • 3
  • 45
  • 57
51
votes
4 answers

What exactly is super in Objective-C?

As far as I know, it's a pointer to the superclass. It's hard-wired with the superclass, and not dynamically figured out at runtime. Would like to know it more in detail... Anyone?
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
51
votes
4 answers

Class is implemented in both. One of the two will be used

I have a project that has a dependency (installed via CocoaPods) using SocketRocket and have imported a static library from HeapAnalytics. Apparently the HeapAnalytics library already uses SocketRocket. I get no errors when compiling, but at runtime…
MrGrinst
  • 970
  • 3
  • 9
  • 20
44
votes
4 answers

Get all methods of an Objective-C class or instance

In Objective-C I can test whether a given class or instance responds to certain selectors. But how can query a class or instance for all its methods or properties of a class (e.g. a list of all methods or properties)?
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
41
votes
3 answers

How can I add properties to an object at runtime?

Is it possible to add properties to an Objective C object at runtime?
cfischer
  • 24,452
  • 37
  • 131
  • 214
35
votes
3 answers

swift: Equivalent objective-c runtime class

What is equivalent swift code for below Objective-C code. I couldn't find swift topic with runtime concept. #import Class class = [self class]; Trying to get class object of self? Update: Tried with below code, got error as…
Mani
  • 17,549
  • 13
  • 79
  • 100
34
votes
4 answers

Intercept Objective-C delegate messages within a subclass

I have a subclass of UIScrollView in which I need to internally respond to scrolling behaviour. However, the viewcontroller will still need to listen to scrolling delegate callbacks, so I can't outright steal the delegate within my component. Is…
Sean Christmann
  • 359
  • 4
  • 4
31
votes
1 answer

objective-c runtime error "Use of undeclared identifier 'objc_property_t'"

I'm trying to get the properties from my class using obj-c runtime approach I found on the answers from here, however I'm getting a lot of warnings/error when coding, do I need to import the library or make something in order to have the obj-c…
Rubs
  • 809
  • 9
  • 21
30
votes
4 answers

Convert a string ("MyExampleClass") into a class name (MyExampleClass)

I want to convert a string to a class name. Imagine that I have a string, which changes, containing a class name, for example, the string "MyExampleClass". Now, I want to create an object of the class MyExampleClass. I have to get the class name…
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
25
votes
10 answers

Get property name as a string

I need a way to pass a property and get the name assigned to it. Any suggestions? @property (nonatomic, retain) MyObject *crazyObject; NSString *str = SOME_WAY_TO_GET_PROPERTY_NAME(crazyObject); // Above method should return @"crazyObject"
aryaxt
  • 76,198
  • 92
  • 293
  • 442
1
2 3
41 42