objective-c-runtime

What is the underlying mechanism for ivar synthesis in the modern Objective C runtime

One of the features of the modern (64 bit OS X and iPhone OS) Objective C runtime is the ability for properties to dynamically synthesize ivars without explicitly declaring them in the class: @interface MyClass : NSObject { // NSString *name; unnecessary on modern runtimes } @property (retain) NSStrng *name; @end @implementation MyC...

How can I add a C-based language to GCC

If I wanted to modify or add my own extensions to C, and add them to the GCC C compiler, what would I need to do? I do not want to propose changes to the language, I want to know how the C compiler actually works. I've had a look at the source code to GCC and it looks as if Objective-C is implemented as a simple parser that generates co...

Compiler warning "not found in protocol(s)" when using [[[UIApplication sharedApplication] delegate] myClass Property]?

I have myClass instantiated by my appDelegate, I have another class, newClass instantiated by myClass. From the newClass instance, I want to access a property in the myClass instance that created it. I have done this by: [[[UIApplication sharedApplication].delegate myClass] property] This works, I can actually get the property but I g...

Objective-C Reflection for generic NSCoding implementation

Is there any means of reflection in Objective-C that would allow you to write generic NSCoding implementations by inspecting the public properties of an object and generating generic implementations of encodeWithCoder: and initWithCoder: . I'm thinking of something like XStream for Java that allows a generic way to serialize and deseria...

Using instance variables with Modern Runtime

I have several years of experience in Obj-c and Cocoa, but am just now getting back into it and the advances of Obj-C 2.0 etc. I'm trying to get my head around the modern runtime and declaring properties, etc. One thing that confuses me a bit is the ability in the modern runtime to have the iVars created implicitly. And of course this i...

NSObjCMessageLoggingEnabled with iPhone 3.0

I'm debugging a program that was written for iPhone OS 2.2.1 and has to be slightly rewritten for 3.0. Having a list of all the Objective-C message calls that are being made, including "behind-the-scenes" calls, would be very useful. I found NSObjCMessageLoggingEnabled on a website, but am not sure if it works with the iPhone. Does ...

IBOutlet like constructs in the Objective-C runtime

My understanding of IBOutlets is that they act as a marker to ivars and properties in Objective-C classes. Is there anything in the Objective-C runtime that would allow one to query whether an ivar or property or a class has been marked with an IBOutlet at runtime? Or does XCode just do something clever with these at compile time? If th...

How do I list all instance variables of a class in Objective-C?

If I have a class, how can I list all its instance variable names? eg: @interface MyClass : NSObject { int myInt; NSString* myString; NSMutableArray* myArray; } I would like to get "myInt", "myString", and "myArray". Is there some way to perhaps get an array of names that I can iterate over? I've tried searching the Obj...

Handling the return value of object_getIvar(id object, Ivar ivar)

1) object_getIvar(id object, Ivar ivar) returns an 'id' if the Ivar is an object eg. if the variabe is an NSString, presumably the id = NSString which contains the value. Is that correct? Or what do I need to do to access the value of the Ivar. 2) if the Ivar is a float/int etc. what will get returned and how do I convert it into someth...

How do I get the int value from object_getIvar(self, myIntVar) as it returns a pointer

if the variable in object_getIvar is a basic data type (eg. float, int, bool) how do I get the value as the function returns a pointer (id) according to the documentation. I've tried casting to an int, int* but when I try to get that to NSLog, I get error about an incompatible pointer type. ...

object_getInstanceVariable works for float, int, bool, but not for double?

I've got object_getInstanceVariable to work as here however it seems to only work for floats, bools and ints not doubles. I do suspect I'm doing something wrong but I've been going in circles with this. float myFloatValue; float someFloat = 2.123f; object_getInstanceVariable(self, "someFloat", (void*)&myFloatValue); works, and myFloat...

Getting the selector from a method directly? NSSelectorFromMethod([[SomeClass someObject] Method]) ?

Is it possible? ...

Obtain list of class methods for an arbitrary class

How can I get the list of class methods for a particular Class? I've tried using the class_copyMethodList function declared in <objc/runtime.h>, but that's only giving me instance methods. I've also found a function that gives me a Method for a class method, but only if I have the selector to the method first (class_getClassMethod). A...

Discovering the user's first and last names?

I am trying to programmatically discover the first and last names of the iPhone user. Is this possible at all? Calling this ... getpwuid( getuid() )->pw_gecos == "Mobile User" ..alas. Iterating over the address book finds all address book records, but doesn't distinguish between the device owner and anyone else (that I can tell). Gi...

Creating an IMP from an Objective-C block

The IMP type in Objective-C represents a function pointer, as far I as understand. Is there any way to make an IMP from a block pointer? Thanks for your ideas. UPDATE: Thanks, @bbum, for your great solution. I haven't modified my code to use it yet, but everyone can see the fruits of my labors here: A new metaclass for Objective-C. ...

Printing arguments sent to forwardInvocation: subclass of NSProxy

I want to intercept messages sent to a proxy object by just printing the selector and arguments. Even if the proxy does not implement them and does not have a target object. Please help. I have looked at several options and Apple docs but they assume that you already know the target object. I want to do this cleanly without memory issues...

Objective-c Runtime Interface Documentation

Are the runtime interfaces(such as __objc_exec_class()) for Objective-C programs specified/documented anywhere, or they are (compiler-)implementation defined? The closest thing to a reference I have found are the GCC headers, but I'd like to know if there is some sort of reference document. ...

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 class_getClassVariable does as opposed to class_getInstanceVariable, and why there is not a class_setClass...

Asterisk usage in Objective-C

I had a question regarding the use of asterisks in Objective-C. Just to be clear: I understand what pointers are and everything in procedural C. I was wondering two things though: 1) Why are all (references to) Objective-C objects pointers? Why not plain variables? (i.e. NSArray array = [[NSArray alloc] init];) 2) Why do you omit the a...

Asterisk usage in Objective-C: related questions

I had a couple questions related to this: http://stackoverflow.com/questions/2035994/asterisk-usage-in-objective-c NSArray array; in a local scope would be an object "allocated" on the stack. NSArray *array; indicates an object backed by a hunk of memory, typically allocated from the heap. How do you know when something is allocat...