Objective C Interview Questions and Answers

What is #import?

It’s a C preprocessor construct to avoid multiple inclusions of the same file.

#import <Object.h>

is an alternative to:

#include <Object.h>

where the .h file is protected itself against multiple inclusions:

#ifndef _OBJECT_H_
…
#define _OBJECT_H_
#endif

What is id?

It’s a generic C type that Objective-C uses for an arbitrary object. For example, a static function that takes one object as argument and returns an object, could be declared as:

static id myfunction(id argument) { ... }

What is an IMP?

It’s the C type of a method implementation pointer, a function pointer to the function that implements an Objective-C method. It is defined to return id and takes two hidden arguments, self and _cmd:

typedef id (*IMP)(id self,SEL _cmd,...);

How do I get an IMP given a SEL?

This can be done by sending a methodFor: message:

IMP myImp = [myObject methodFor:mySel];

How can I link a C++ library into an Objective-C program?

You have two options: either use the Apple compiler or use the POC. The former accepts a mix of C++ and Objective-C syntax (called Objective-C++), the latter compiles Objective-C into C and then compiles the intermediate code with a C++ compiler.

How do I make a static method?

Methods are always implemented in Objective-C as static functions. The only way to obtain the IMP (implementation pointer) of a method is through the runtime (via methodFor: and friends) because the function itself is static to the file that implements the method.

How do I include X Intrinsics headers into an Objective-C file?

To avoid a conflict between Objective-C’s Object and the X11/Object, do the following:

#include <Object.h>
#define Object XtObject
#include <X11/Intrinsic.h>
#include <X11/IntrinsicP.h>
#undef Object

How do I allocate an object on the stack?

To allocate an instance of ‘MyClass’ on the stack:

MyClass aClass = [MyClass new];

What’s the class of a constant string?

It’s an NXConstantString.

NXConstantString *myString = @"my string";
Related Post