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

Method swizzling for "alloc"?

I try to use method swizzling for catching "alloc" for NSObject generic. NSObject *obj = [[NSObject alloc] init]; UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; NSString *str = [[NSString alloc] init]; [...] This is the implementation…
elp
  • 8,021
  • 7
  • 61
  • 120
8
votes
1 answer

what are class_setIvarLayout and class_getIvarLayout?

I am trying to dynamic create objc class at runtime and I need to add some Ivar to the created class. I found class_addIvar which should do what I want, but I also found these…
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
8
votes
2 answers

Using class_getInstanceMethod - where is the method implemented in the class hierarchy?

Is it possible to find where in a class hierarchy the method retrieved by class_getInstanceMethod is coming from? For example, say Class A implements myMethod. Now say i've subclassed Class A in Class A1. If I call class_getInstanceMethod(ClassA1,…
Sean Danzeiser
  • 9,141
  • 12
  • 52
  • 90
8
votes
3 answers

How to allocate a new object without Foundation?

I want to construct a simple Objective C program without Foundation. I tried: #include @interface Foo{ char * bar; } -(void)hello; @end @implementation Foo -(void)hello { printf("Hello world!"); } @end int main(){ Foo * foo…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
8
votes
2 answers

What is `objc_msgSend_fixup`, exactly?

I'm messing around with the Objective-C runtime, trying to compile objective-c code without linking it against libobjc, and I'm having some segmentation fault problems with a program, so I generated an assembly file from it. I think it's not…
LuisABOL
  • 2,951
  • 4
  • 25
  • 57
7
votes
2 answers

How to interpret objective-c type specifier (e.g. returned by method_copyReturnType())?

Given I have a type specifier as returned by method_copyReturnType(). In the GNU runtime delivered with the GCC there are various methods to work with such a type specifier like objc_sizeof_type(), objc_alignof_type() and others. When using the…
Tilo Prütz
  • 1,766
  • 3
  • 16
  • 27
7
votes
3 answers

How to get runtime Block type metadata in Objective-c?

I'm writing a class where you register an object and a property to observe. When the property gets set to something non-nil, a registered callback selector is called (like target-action). The selector may have three different signatures, and the…
Påhl Melin
  • 657
  • 1
  • 7
  • 14
7
votes
1 answer

Get the object which called a method

If I have a call from within a random class like this: @implementation SomeClass - (void) classMethodFoo { int a = [SomeSingleton sharedInstance].aValue; } @end Inside SomeSingleton sharedInstance, is there a way to get a reference to the…
Egil
  • 4,265
  • 4
  • 20
  • 13
7
votes
1 answer

How do I list the Protocols an Object Conforms to?

Using the Objective-C runtime, I can get a list of all of the @objc protocols an object conforms to: let obj = NSObject() var pc: UInt32 = 0 let plist = class_copyProtocolList(object_getClass(obj), &pc) print("\(obj.dynamicType) conforms to \(pc)…
JAL
  • 41,701
  • 23
  • 172
  • 300
7
votes
2 answers

Proper way of method swizzling in objective-C

Currently experimenting with method swizzling in Objective-C and I have a question. I am trying to understand the proper way to method swizzle and after researching online I stumbled upon this NSHipster…
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
7
votes
1 answer

Find out if an Objective-C class overrides a method

How can I find out, at runtime, if a class overrides a method of its superclass? For example, I want to find out if a class has it's own implementation of isEqual: or hash, instead of relying on a super class.
cfischer
  • 24,452
  • 37
  • 131
  • 214
7
votes
2 answers

Using dispatch_once in method swizzling

In NSHipter's article on method swizzling, it says "Swizzling should always be done in a dispatch_once." Why is this necessary since +load happens only once per class?
Boon
  • 40,656
  • 60
  • 209
  • 315
7
votes
1 answer

Is there a way to inject an OS X system framework system-wide?

I need to get notified when a app (including system app/server) calls System Framework (CoreServices.framework). I am not sure whether Code Injection works on system-wide frameworks. Is it possible to replace a system framework with my own copy, and…
xhan
  • 6,057
  • 4
  • 33
  • 47
7
votes
1 answer

Swift : Alternative to .classForCoder()

Given the following code: return TyphoonDefinition.withClass(AppDelegate.classForCoder()) { (definition) in definition.injectProperty("assembly") }) . . . it is necessary to use .classForCoder(), as .class() is struck out.…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
7
votes
1 answer

NSProxy pretending to be Class doesn't handle respondsToSelector in 64-bit runtime

In OCMockito, test doubles are implemented with NSProxy. A double standing in for an instance implements -respondsToSelector: as follows: - (BOOL)respondsToSelector:(SEL)aSelector { return [_mockedClass…
Jon Reid
  • 20,545
  • 2
  • 64
  • 95