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
11
votes
7 answers

Swift closure as AnyObject

Im trying to use this method: class_addMethod() which in Obj-c is used like this: class_addMethod([self class], @selector(eventHandler), imp_implementationWithBlock(handler), "v@:"); And Im using it like this in…
Arbitur
  • 38,684
  • 22
  • 91
  • 128
11
votes
1 answer

Why the objc runtime function `class_addMethod()` adds the implementation as instance and class method when the target class is `NSObject`?

When I use objc runtime function class_addMethod() to inject an implementation to NSObject's instance selector, it actually inject the implementation to the instance selector AND the class selector: @implementation HelloWorldClass - (void)…
Xaree Lee
  • 3,188
  • 3
  • 34
  • 55
11
votes
2 answers

What does class_getClassVariable() do?

If instance variables belong to an instance of a class, class variables would belong to an instance of a metaclass, I should think. But my experience with the Objective-C metaclass tells me that this is unlikely. I'm wondering what…
11
votes
1 answer

What's wrong with using a category on NSObject to provide a default protocol implementation?

I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling…
11
votes
3 answers

Objective-C: What is a lazy class?

Looking at the Objective-C runtime library source code, particularly at objc-runtime-new.mm, I saw some functions and even comments which referred to lazy and non-lazy classes. It seems that classes that don't have a +load method are called lazy…
LuisABOL
  • 2,951
  • 4
  • 25
  • 57
11
votes
2 answers

How does the Objective-C runtime retrieve the list of classes and methods?

If I get the following Objective-C source file: // test.m #import @interface MySuperClass: Object { } -(void) myMessage1; @end @implementation MySuperClass -(void) myMessage1 { } @end @interface MyClass: MySuperClass…
LuisABOL
  • 2,951
  • 4
  • 25
  • 57
11
votes
3 answers

C As Principal Class Or; A Cocoa App Without ObjC

For those who question my sanity/time-wasting/ability/motives, this thing is a port of the "Foundation With A Spoon" project, now infamous for being an all-C iOS app. So, I've got a c file raring to go and be the main class behind an all-C…
CodaFi
  • 43,043
  • 8
  • 107
  • 153
10
votes
2 answers

Method swizzling for property in swift

While it is possible to replace setMyProperty: method in obj-c, I'm wondering how to do it in swift? For example I want to replace UIScrollView::setContentOffset:: let originalSelector: Selector = #selector(UIScrollView.setContentOffset) let…
brigadir
  • 6,874
  • 6
  • 46
  • 81
10
votes
2 answers

What is the best way to avoid duplicate symbols in project that will use my iOS framework and one of the dependencies?

Here is a quotation from the other post: I'm working in a iOS project that includes a static library created by another company. The library include an old version of AFNeworking and I don't have any source files. Now i need to use a more recent…
10
votes
2 answers

iOS / Objective-C: Correct Way of Obtaining Meta Class Object

Which from the following is the correct way of obtaining the meta class? Class myMetaClass = objc_getMetaClass("NSString"); Or: Class myMetaClass = object_getClass([NSString class]); Are they both any different? As mentioned in another post that…
Unheilig
  • 16,196
  • 193
  • 68
  • 98
9
votes
2 answers

Objective-C associated objects leaking under ARC

I have encountered with a strange objc_setAssociatedObject behavior under ARC. Consider the following code: static char ASSOC_KEY; @interface DeallocTester : NSObject @end @implementation DeallocTester - (void) dealloc { NSLog(@"DeallocTester…
iHunter
  • 6,205
  • 3
  • 38
  • 56
9
votes
1 answer

Getting list of class methods

I'm looking for a way to get a static methods list for a certain class. I only get a list of instance methods with the runtime function class_copyMethodList(). Is there a way to list static methods?
Laurynas
  • 3,829
  • 2
  • 32
  • 22
9
votes
1 answer

Is there any problem using self.property = nil in dealloc?

I know declared property generates accessor method which is someway just syntax sugar. I found quite a lot people use self.property = nil in their dealloc method. 1) In Apple's Memory Management document, p23 It says: The only places you shouldn’t…
Jimmy
  • 1,094
  • 10
  • 22
9
votes
2 answers

Set limit to UITextField in ObjectiveC

I am creating login screen using Objective C in which i want to implement validation for User name(E-Mail) and password.How to implement this in very simple way.
Mohith P
  • 585
  • 5
  • 14
9
votes
1 answer

Deploying to OS X 10.6 and "-fobj-arc is not supported on platforms using the legacy runtime"

Background: I'm building an app for OS X with deployment target of 10.6. I have not converted my app to ARC completely, but I am adding a few new classes which would benefit from ARC, so I have set the -fobj-arc compiler flag for those…