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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With