Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Xcode: Compiler error accessing Ivar in Objective-C

I am working in a large existing objective-c codebase, writing unit tests at the moment. The project uses instance variables widely.

I wrote a little method to grab the ivar for something. It worked in another unit test in the same project, but isn't working in this case.

Code:

-(id)getObjectForIvarNamed:(NSString *)ivarNameString
 {
    const char *ivarName = [ivarNameString UTF8String];

    Ivar ivarValue = class_getInstanceVariable([textFieldOverlay class], ivarName);
    id objectAtIvar = object_getIvar(textFieldOverlay, ivarValue);

    return objectAtIvar;
}

The compiler complains at the line starting Ivar ivarValue with the following error:

"Declaration of 'Ivar' must be imported from module 'ObjectiveC.runtime' before it is required"

A web search for this error code comes up with zero hits. Not sure why it works in another file, the headers all look the same between the two XCUnitTest classes.

like image 807
Nate Birkholz Avatar asked Feb 27 '15 21:02

Nate Birkholz


1 Answers

Never fails, found the answer as soon as I had typed the question. But, I hadn't posted yet. Because this error was impossible to find in a web search, I figured I should post it, so there is an instance out there. The project's class I was testing in the working test class imports the objective-c runtime header, I thought to check the tested class after I typed the question up. I thought it was a header issue but didn't see it in the imported class. Learning moment.

To import the objective-c runtime header and eradicate this warning, add #import <objc/runtime.h> to the header. Done, and done.

like image 183
Nate Birkholz Avatar answered Sep 28 '22 07:09

Nate Birkholz