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
6
votes
1 answer

Prevent Method Swizzling Objective-c

I am trying to search to prevent the Method Swizzle in my current library but more or less every documentation or blog post that i found are about how to implement Swizzling. There are couple of question that i have regarding Method swizzling that i…
Akhilesh Sharma
  • 1,580
  • 1
  • 17
  • 29
6
votes
5 answers

Are selectors in Objective-C just another way to send a message to an object?

Are selectors in Objective-C just another way to send a message to an object? I really don't understand why or how to use them.
lampShade
  • 4,361
  • 8
  • 34
  • 50
6
votes
1 answer

swift method_exchangeImplementations not work

swift code is below: func swizzleMethod() { let method:Method = class_getInstanceMethod(object_getClass(self), Selector("function1")) self.function1() let swizzledMethod:Method = class_getInstanceMethod(object_getClass(self),…
Tornado
  • 131
  • 6
6
votes
1 answer

Is it possible to introspect the array's type in Objective-C Generics -- Xcode 7+

I use this to get the class for a property name: - (Class)gm_classForPropertyName:(NSString *)propertyName { objc_property_t prop = class_getProperty([self class], propertyName.UTF8String); const char * attr = property_getAttributes(prop); …
Logan
  • 52,262
  • 20
  • 99
  • 128
6
votes
1 answer

What is the objc_selector implementation?

I've found that the SEL type has the next definition: typedef struct objc_selector *SEL; But I can't find how is objc_selector implemented. Okay, if we have the next code #import int main(int argc, const char * argv[]) { …
Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
6
votes
8 answers

Interpret Objective C scripts at runtime on iPhone?

Is there anyway to load an objective c script at runtime, and run it against the classes/methods/objects/functions in the current iPhone app? MAJOR NOTE: The major reason I'd like to do this is to allow me to rapidly prototype an application, and…
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
6
votes
1 answer

How do I lookup a string constant at runtime in Objective-C?

My company develops an advertising SDK that mediates other ad networks. At runtime, it checks if the other ad networks are present by using NSClassFromString, and sends those classes messages if they're present. This works fine for Objective-C…
MaxGabriel
  • 7,617
  • 4
  • 35
  • 82
6
votes
2 answers

Retrieving property list of a class in iOS

I am trying to retrieve a list of all the properties that my class or any of its subclasses define. The following code snippet is the code that I have been using, and it has worked properly all the way until the recent iOS8 beta 4. if(!dictionary)…
rvijay007
  • 1,357
  • 12
  • 20
6
votes
1 answer

How does the Objective-C runtime instantiate the root metaclass and other class descriptions?

I'm trying to implement a basic object-oriented ANSI C runtime and using Objective-C as a guide. They're seems to be three parts. A Class Description, Class Interface, and Class Implementation. In order for the Class Interface to be instantiated,…
Anderson
  • 327
  • 3
  • 10
6
votes
4 answers

Objective-C property assignment returns the assigned value?

Say I have the following: @interface MyClass : NSObject { NSString* _foobar; } @property (nonatomic, retain) NSString* foobar; @end @implementation MyClass @dynamic foobar; - (void) setFoobar:(NSString*)fbSet; { [_foobar release]; _foobar = [fbSet…
rpj
  • 2,380
  • 2
  • 17
  • 30
6
votes
2 answers

Decode Class from @encoded type string

Objective-C's @encode produces C strings to represent any type, including primitives, and classes, like so: NSLog(@"%s", @encode(int)); // i NSLog(@"%s", @encode(float)); // f NSLog(@"%s", @encode(CGRect)); //…
Greg
  • 9,068
  • 6
  • 49
  • 91
6
votes
2 answers

Any gotchas with objc_setAssociatedObject and objc_getAssociatedObject?

I’m looking into ways to add a property (an integer in this case) to all UIView instances, whether they are subclassed or not. Is using objc_setAssociatedObject() and objc_getAssociatedObject() within a category the appropriate, Apple-endorsed way…
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
6
votes
1 answer

Find out if an object is a class object rather than an instance of a class

Using the Objective-C runtime library, how do we find out if an object is a class object rather than an instance of a class?
Boon
  • 40,656
  • 60
  • 209
  • 315
6
votes
2 answers

object_getClass(obj) and [obj class] give different results

I get two different object instances when calling object_getClass(obj) and [obj class]. Any idea why? Class cls = object_getClass(obj); Class cls2 = [obj class]; (lldb) po cls $0 = 0x0003ca00 Test (lldb) po cls2 $1 = 0x0003ca14 Test (lldb)
Boon
  • 40,656
  • 60
  • 209
  • 315
6
votes
2 answers

Understanding Objective-C runtime

This is an excerpt from Objective-C runtime programming guide: When a new object is created, memory for it is allocated, and its instance variables are initialized. First among the object’s variables is a pointer to its class structure. This…
Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161