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

How to detect if a property is an IBOutlet programmatically at runtime?

I am setting up unit tests on my project to make sure that all UIViewController IBOutlets are connected to their respective Xib objects (i.e., are not nil after viewDidLoad.) I was considering applying a protocol to these UIViewControllers with a…
5
votes
3 answers

objc_setAssociatedObject retain atomic or nonatomic

When I use objc_setAssociatedObject, I know whether to use retain or assign, but I don't know how to decide between OBJC_ASSOCIATION_RETAIN and OBJC_ASSOCIATION_RETAIN_NONATOMIC. When should one or the other be used?
Jeff
  • 2,659
  • 1
  • 22
  • 41
5
votes
1 answer

Get back to sel_getUid()'s original behaviour

TL;DR: How does one check that selector with given name was registered, without actually registering it? Thanks! Hi, I have an Objective-C application and bunch of NSObjects that are exported into Lua state via simple proxy library written in objc.…
user3125367
  • 2,920
  • 1
  • 17
  • 17
5
votes
2 answers

NSClassFromString using a Swift File

I have written a class in Swift in my existing Objective-C project. So far, the bridging has worked very well. I do have a method however, where I generate a class at runtime using NSClassFromString(). When the string is my Swift class name, it…
ahoang
  • 139
  • 1
  • 9
5
votes
2 answers

Objective-c IOS arm64 method swizzling fail to call original method

I use standard method swizzling on ARMv7 IOS devices and it works perfect to me. But when I compile my code for arm64 - it fails to call original method from new method Main purpose of my swizzling - to use parameter from internal method of my…
5
votes
1 answer

How do I use objective-c-runtime's object_getIvar & object_setIvar in swift?

Does anybody know why I get BAD_ACCESS on getting & setting of my iVars with the following code ? class myClass: NSObject { var model = "Unspecified" override init() { super.init() var key: NSString = "model" var…
Tycho Pandelaar
  • 7,367
  • 8
  • 44
  • 70
5
votes
1 answer

Log Objective-c message sends on a device

When running an iOS app in the simulator, there's an environment variable NSObjCMessageLoggingEnabled that causes all objc_msgSend calls to be logged out to a file. (Detailed explanation). I'm trying to trace out all the messages that are being sent…
funroll
  • 35,925
  • 7
  • 54
  • 59
5
votes
1 answer

In objective-c , how can an object return a different proxy object when itself is assigned as a delegate it implements

I have an object which implements various protocols (like 10 different ones). For example @interface MyClass @end @implementation /// a whole bunch of methods for the…
Avba
  • 14,822
  • 20
  • 92
  • 192
5
votes
2 answers

Swift : Why is the class method struck out

As long as a Swift class extends from NSObject we can pass it to the Objective-C runtime and ask it to introspect it for us. We have three options: class classForCoder classForKeyedArchiver . . however class is struck out. (See image). Why is…
Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
5
votes
2 answers

Get property name of object as string in Objective-C

Suppose you have a class "Foo" with property "testProperty". The aim is to get property name (not value) as NSString. The question is probably duplicate of Get property name as a string . But that answers did not help me because: I don't need all…
razor28
  • 1,077
  • 10
  • 17
5
votes
2 answers

iOS / Objective-C - struct objc_class * and struct objc_object *

My understanding is that objects and classes in Objective-C are just structs. Respectively they are just: struct objc_class * and: struct objc_object * Question #1: objc_msgSend(id self, SEL _cmd); id as far as I know is of type struct…
Unheilig
  • 16,196
  • 193
  • 68
  • 98
5
votes
2 answers

ios 6 and 7 doesnt return same results

It seems that our apps which use getPropertyType(..) are failing under ios7. For whatever reason, getPropertyType(..) on for example a NSString property returns NSString$'\x19\x03\x86\x13 as the type, instead of just NSString, and also instead of…
tskulbru
  • 2,336
  • 1
  • 20
  • 38
5
votes
3 answers

Is dispatch_once overkill inside of +[NSObject initialize]?

If I create a singleton inside of +[NSObject initialize], do I need to put my code inside a dispatch_once block like so? static NSObject * Bar; @implementation Foo + (void)initialize { if (self == [Foo class]) { static dispatch_once_t…
5
votes
2 answers

Swizzling a method with variable arguments and forward the message - Bad Access

I'm implementing a "Code Injector Class", that through method swizzling can give you the possibility to do something like this: FLCodeInjector *injector = [FLCodeInjector injectorForClass:[self class]]; [injector…
LombaX
  • 17,265
  • 5
  • 52
  • 77
5
votes
1 answer

Introspect the current block context, à la _cmd inside a method

For me, Objective-C's ability to react, describe, and mess-with its surroundings is where it's at. This starts, at a fundamental level, with an unwavering ability to refer to _cmd, at any point, and get the current SEL. From there, it is up to you…