IOS Questions

iphone Interview Questions part-11

111- Objective-C.

Objective-C is a very dynamic language. Its dynamism frees a program from compile-time and link-time constraints and shifts much of the responsibility for symbol resolution to runtime, when the user is in control. Objective-C is more dynamic than other programming languages because its dynamism springs from three sources:

Dynamic typing—determining the class of an object at runtime

Dynamic binding—determining the method to invoke at runtime

Dynamic loading—adding new modules to a program at runtime

112- Objective-C vs C/C++.

· The Objective-C class allows a method and a variable with the exact same name. In C++, they must be different.

· Objective-C does not have a constructor or destructor. Instead it has init and dealloc methods, which must be called explicitly.

· Objective-C uses + and – to differentiate between factory and instance methods, C++ uses static to specify a factory method.

· Multiple inheritance is not allowed in Obj-C, however we can use protocol to some extent.

· Obj-C has runtime binding leading to dynamic linking.

· Obj-C has got categories.

· Objective-C has a work-around for method overloading, but none for operator overloading.

· Objective-C also does not allow stack based objects. Each object must be a pointer to a block of memory.

· In Objective-C the message overloading is faked by naming the parameters. C++ actually does the same thing but the compiler does the name mangling for us. In Objective-C, we have to mangle the names manually.

· One of C++’s advantages and disadvantages is automatic type coercion.

· Another feature C++ has that is missing in Objective-C is references. Because pointers can be used wherever a reference is used, there isn’t much need for references in general.

· Templates are another feature that C++ has that Objective-C doesn’t. Templates are needed because C++ has strong typing and static binding that prevent generic classes, such as List and Array.

113-Appilcation Kit/App kit.

The Application Kit is a framework containing all the objects you need to implement your graphical, event-driven user interface: windows, panels, buttons, menus, scrollers, and text fields. The Application Kit handles all the details for you as it efficiently draws on the screen, communicates with hardware devices and screen buffers, clears areas of the screen before drawing, and clips views.

You also have the choice at which level you use the Application Kit:

· Use Interface Builder to create connections from user interface objects to your application objects.

· Control the user interface programmatically, which requires more familiarity with AppKit classes and protocols.

· Implement your own objects by subclassing NSView or other classes.

114-Foundation Kit.

The Foundation framework defines a base layer of Objective-C classes. In addition to providing a set of useful primitive object classes, it introduces several paradigms that define functionality not covered by the Objective-C language. The Foundation framework is designed with these goals in mind:

· Provide a small set of basic utility classes.

· Make software development easier by introducing consistent conventions for things such as deallocation.

· Support Unicode strings, object persistence, and object distribution.

· Provide a level of OS independence, to enhance portability.

115-Dynamic and Static Typing.

Static typed languages are those in which type checking is done at compile-time, whereas dynamic typed languages are those in which type checking is done at run-time.

Objective-C is a dynamically-typed language, meaning that you don’t have to tell the compiler what type of object you’re working with at compile time. Declaring a type for a varible is merely a promise which can be broken at runtime if the code leaves room for such a thing. You can declare your variables as type id, which is suitable for any Objective-C object.

116-Selectors

In Objective-C, selector has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector. You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.

[friend performSelector:@selector(gossipAbout:) withObject:aNeighbor];

is equivalent to:

[friend gossipAbout:aNeighbor];

117-Class Introspection

· Determine whether an objective-C object is an instance of a class

[obj isMemberOfClass:someClass];

· Determine whether an objective-C object is an instance of a class or its descendants

[obj isKindOfClass:someClass];

· The version of a class

[MyString version]

· Find the class of an Objective-C object

Class c = [obj1 class]; Class c = [NSString class];

· Verify 2 Objective-C objects are of the same class

[obj1 class] == [obj2 class]

118- Proxy

As long as there aren’t any extra instance variables, any subclass can proxy itself as its superclass with a single call. Each class that inherits from the superclass, no matter where it comes from, will now inherit from the proxied subclass. Calling a method in the superclass will actually call the method in the subclass. For libraries where many objects inherit from a base class, proxying the superclass can be all that is needed.

119- Why category is better than inheritance?

If category is used, you can use same class, no need to remember a new class-name. Category created on a base class is available on sub classes.

120-Formal Protocols

Formal Protocols allow us to define the interface for a set of methods, but implementation is not done. Formal Protocols are useful when you are using DistributedObjects, because they allow you to define a protocol for communication between objects, so that the DO system doesn’t have to constantly check whether or not a certain method is implemented by the distant object.