This is continuation for  iOS interview questions with answers.

 *Q: What is Controller Object?

A:

A controller object acts as a coordinator or as an intermediary between one or more view objects and one or more model objects. In the Model-View-Controller design pattern, a controller object (or, simply, a controller) interprets user actions and intentions made in view objects—such as when the user taps or clicks a button or enters text in a text field—and communicates new or changed data to the model objects.image: Art/controller_object.jpg

 When model objects change—for example, the user opens a document stored in the file system—it communicates that new model data to the view objects so that they can display it. Controllers are thus the conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also set up and coordinate tasks for an application and manage the life cycles of other objects. The Cocoa frameworks offer three main controller types: coordinating controllers, view controllers (on iOS), and mediating controllers (on OS X).

*Q: What is Wildcard App IDs?

A:

A wildcard app ID allows you to use an app ID to match multiple apps; wildcard app IDs are useful when you first start developing new apps because you don’t need to create a separate app ID for each app. However, wildcard app IDs can’t be used to provision an app that uses APNS, In App Purchase, or Game Center.

A wildcard app ID omits some or all of the bundle ID in the search string and replaces that portion with an asterisk character (*). The asterisk must always appear as the last character in the bundle search string.

When you use a wildcard app ID, characters preceding the asterisk (if any) must match the characters in the bundle ID, exactly as they would for an explicit app ID. The asterisk matches all remaining characters in the bundle ID. Further, the asterisk must match at least one character in the bundle ID. This table shows an example search string and how it matches some bundle IDs but not others.

image: Art/app_ids_examples.jpg

If an app id uses an * as the bundle ID, then the search string matches any bundle ID.

 

*Q: What is categories in iOS?

A:

We use categories to define additional methods of an existing class—even one whose source code is unavailable to you—without subclassing. You typically use a category to add methods to an existing class, such as one defined in the Cocoa frameworks. The added methods are inherited by subclasses and are indistinguishable at runtime from the original methods of the class. You can also use categories of your own classes to:

  • Distribute the implementation of your own classes into separate source files—for example, you could group the methods of a large class into several categories and put each category in a different file.
  • Declare private methods.

You add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are an extension to a class declared elsewhere, not a new class.

Refer: Categories and Extensions

*Q: What is Delegation in iOS?

A:

Delegation is a design pattern in which one object sends messages to another object—specified as its delegate—to ask for input or to notify it that an event is occurring. Delegation is often used as an alternative to class inheritance to extend the functionality of reusable objects. For example, before a window changes size, it asks its delegate whether the new size is ok. The delegate replies to the window, telling it that the suggested size is acceptable or suggesting a better size. (For more details on window resizing, see the [block]2[/block] message.)

Delegate methods are typically grouped into a protocol. A protocol is basically just a list of methods. The delegate protocol specifies all the messages an object might send to its delegate. If a class conforms to (or adopts) a protocol, it guarantees that it implements the required methods of a protocol. (Protocols may also include optional methods).In this application, the application object tells its delegate that the main startup routines have finished by sending it the [block]3[/block] message. The delegate is then able to perform additional tasks if it wants.

*Q: How can we achieve singleton pattern in iOS?

A:

The Singleton design pattern ensures a class only has one instance, and provides a global point of access to it. The class keeps track of its sole instance and ensures that no other instance can be created. Singleton classes are appropriate for situations where it makes sense for a single object to provide access to a global resource.

Several Cocoa framework classes are singletons.

They include [block]4[/block]NSWorkspaceNSApplication, and, in UIKit, [block]5[/block]. A process is limited to one instance of these classes. When a client asks the class for an instance, it gets a shared instance, which is lazily created upon the first request.Refer: Singleton Pattren

*Q: What is delegate pattern in iOS?

A:

Delegation is a mechanism by which a host object embeds a weak reference (weak in the sense that it’s a simple pointer reference, unretained) to another object—its delegate—and periodically sends messages to the delegate when it requires its input for a task. The host object is generally an “off-the-shelf” framework object (such as an NSWindow or [block]7[/block]object) that is seeking to accomplish something, but can only do so in a generic fashion. The delegate, which is almost always an instance of a custom class, acts in coordination with the host object, supplying program-specific behavior at certain points in the task (see Figure 4-3). Thus delegation makes it possible to modify or extend the behavior of another object without the need for subclassing.

*Q: What are all the difference between categories and subclasses? Why should we go to subclasses?

A:

Category is a feature of the Objective-C language that enables you to add methods (interface and implementation) to a class without having to make a subclass. There is no runtime difference—within the scope of your program—between the original methods of the class and the methods added by the category. The methods in the category become part of the class type and are inherited by all the class’s subclasses.As with delegation, categories are not a strict adaptation of the Decorator pattern, fulfilling the intent but taking a different path to implementing that intent. The behavior added by categories is a compile-time artifact, and is not something dynamically acquired. Moreover, categories do not encapsulate an instance of the class being extended.The Cocoa frameworks define numerous categories, most of them informal protocols . Often they use categories to group related methods. You may implement categories in your code to extend classes without subclassing or to group related methods. However, you should be aware of these caveats:

  • You cannot add instance variables to the class.
  • If you override existing methods of the class, your application may behave unpredictably.

 

*Q: What is notification in iOS?

A:

The notification mechanism of Cocoa implements one-to-many broadcast of messages based on the Observer pattern. Objects in a program add themselves or other objects to a list of observers of one or more notifications, each of which is identified by a global string (the notification name). The object that wants to notify other objects—the observed object—creates a notification object and posts it to a notification center. The notification center determines the observers of a particular notification and sends the notification to them via a message. The methods invoked by the notification message must conform to a certain single-parameter signature. The parameter of the method is the notification object, which contains the notification name, the observed object, and a dictionary containing any supplemental information.Posting a notification is a synchronous procedure. The posting object doesn’t regain control until the notification center has broadcast the notification to all observers. For asynchronous behavior, you can put the notification in a notification queue; control returns immediately to the posting object and the notification center broadcasts the notification when it reaches the top of the queue.Regular notifications—that is, those broadcast by the notification center—are intraprocess only. If you want to broadcast notifications to other processes, you can use the istributed notification center and its related API.

*Q:What is the difference between delegates and notifications?

A:

We can use notifications for a variety of reasons. For example, you could broadcast a notification to change how user-interface elements display information based on a certain event elsewhere in the program. Or you could use notifications as a way to ensure that objects in a document save their state before the document window is closed. The general purpose of notifications is to inform other objects of program events so they can respond appropriately.But objects receiving notifications can react only after the event has occurred. This is a significant difference from delegation. The delegate is given a chance to reject or modify the operation proposed by the delegating object. Observing objects, on the other hand, cannot directly affect an impending operation.

*Q:What is posing in iOS?

A:

Objective-C permits a class to entirely replace another class within an application. The replacing class is said to “pose as” the target class. All messages sent to the target class are then instead received by the posing class. There are some restrictions on which classes can pose:
  • A class may only pose as one of its direct or indirect superclasses
  • The posing class must not define any new instance variables which are absent from the target class (though it may define or override methods).
  • No messages must have been sent to the target class prior to the posing.
Posing, similarly to categories, allows globally augmenting existing classes. Posing permits two features absent from categories:
  • A posing class can call overridden methods through super, thus incorporating the implementation of the target class.
  • A posing class can override methods defined in categories.

 

*Q:What is atomic and nonatomic? Which one is safer? Which one is default?

A:

You can use this attribute to specify that accessor methods are not atomic. (There is no keyword to denote atomic.)

nonatomic
Specifies that accessors are nonatomic. By default, accessors are atomic.

Properties are atomic by default so that synthesized accessors provide robust access to properties in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently.

If you specify strongcopy, or retain and do not specify nontoxic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly.

Markup and Deprecation

Properties support the full range of C-style decorators. Properties can be deprecated and support __attribute__ style markup:

@property CGFloat x
AVAILABLE_MAC_OS_X_VERSION_10_1_AND_LATER_BUT_DEPRECATED_IN_MAC_OS_X_VERSION_10_4;
@property CGFloat y __attribute__((…));

 

*Q: What is run loop in iOS ?

A:

Run loops are part of the fundamental infrastructure associated with threads. A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.

Run loop management is not entirely automatic. You must still design your thread’s code to start the run loop at appropriate times and respond to incoming events. Both Cocoa and Core Foundation provide run loop objects to help you configure and manage your thread’s run loop. Your application does not need to create these objects explicitly; each thread, including the application’s main thread, has an associated run loop object. Only secondary threads need to run their run loop explicitly, however. In both Carbon and Cocoa applications, the main thread automatically sets up and runs its run loop as part of the general application startup process.

*Q: What isDynamic typing?

A:

A variable is dynamically typed when the type of the object it points to is not checked at compile time. Objective-C uses the id data type to represent a variable that is an object without specifying what sort of object it is. This is referred to as dynamic typing.

Dynamic typing contrasts with static typing, in which the system explicitly identifies the class to which an object belongs at compile time. Static type checking at compile time may ensure stricter data integrity, but in exchange for that integrity, dynamic typing gives your program much greater flexibility. And through object introspection (for example, asking a dynamically typed, anonymous object what its class is), you can still verify the type of an object at runtime and thus validate its suitability for a particular operation.

*Q: What is push notification?How it works?

A:Push-Overview

 Refer apple documentation

*Q: What is the configuration file name in iOS explain in brief ? (Or) What is plist file and explain about it is usage?

A:

A property list is a representation of a hierarchy of objects that can be stored in the file system and reconstituted later. Property lists give applications a lightweight and portable way to store small amounts of data. They are hierarchies of data made from specific types of objects—they are, in effect, an object graph. Property lists are easy to create programmatically and are even easier to serialize into a representation that is persistent. Applications can later read the static representation back into memory and recreate the original hierarchy of objects. Both Cocoa Foundation and Core Foundation have APIs related to property list serialization and deserialization.

Property List Types and Objects

Property lists consist only of certain types of data: dictionaries, arrays, strings, numbers (integer and float), dates, binary data, and Boolean values. Dictionaries and arrays are special types because they are collections; they can contain one or multiple data types, including other dictionaries and arrays. This hierarchical nesting of objects creates a graph of objects. The abstract data types have corresponding Foundation classes, Core Foundation types, and XML elements for collection objects and value objects.

 *Q:When will be the autorelease object released?

A:

Once the pool recives drain message.

*Q:Consider we are implementing our own thread with lot of autoreleased object. Is it mandatory to use autorelease pool on this scenario if yes/no why?

A:

YES.

 *Q:  Have you ever used automated unit test framework in iOS? Explain in short?

A:

 

*Q: What are all the difference between iOS3, iOS4 and iOS5?

A:

Refer:  Apple iOS Versions and diffrences

*Q: Is there any garbage collector concept available in iOS?

A:

No, Manual memory management or ARC.

 *Q: What is difference between synchronous and asynchronous in web request?

A:

 

*Q:What are all the instruments available in Xcode?

A:

*Q:What is the difference between copy & retain? When can we go for copy and when can we go for retain?

A:

As always  leave your comments & suggestion below for iPhone interview questions.
  • NeoAbi

    Thank u soo much…

  • abhinav trivedi

    This is a set of lot of good questions for interview. Thanks you so much…..:)

  • Subrat

    What is difference between synchronous and asynchronous in web request

    +”Synchronous” essentially means “in order.” Basically, when you do a synchronous operation, everything that comes later has to wait for the operation to finish before they can start.

    ex- //Do something

    dispatch_sync(queue, ^{

    //Do something else

    });

    //Do More Stuff

    +”asynchronous” means “not in order.” When you do something asynchronously, the following code can immediately run and the asynchronous operation will be run…sometime. It might be run in parallel with the rest of the code on another thread. It might simply be scheduled for some other time on the same thread.

    ex- // Methods gets called in different thread and does not block the current thread.

    [NSURLConnection sendAsynchronousRequest:request

    queue:queue

    completionHandler:

    ^(NSURLResponse *response, NSData *data, NSError *error) {

    }];

    +”Synchronous” essentially means “in order.” Basically, when you do a synchronous operation, everything that comes later has to wait for the operation to finish before they can start.

    ex- //Do something

    dispatch_sync(queue, ^{

    //Do something else

    });

    //Do More Stuff

    +”asynchronous” means “not in order.” When you do something asynchronously, the following code can immediately run and the asynchronous operation will be run…sometime. It might be run in parallel with the rest of the code on another thread. It might simply be scheduled for some other time on the same thread.

    ex- // Methods gets called in different thread and does not block the current thread.

    [NSURLConnection sendAsynchronousRequest:request

    queue:queue

    completionHandler:

    ^(NSURLResponse *response, NSData *data, NSError *error) {

    }];

  • Subrat

    Copy Vs Retain

    copy

    -copy is required when the object is mutable.

    -copy specifies the new value should be sent -copy on assignment and the old value sent -release.

    -copy is like retain returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments.

    -if you use copy then you still need to release that in dealloc.

    -Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any changes made by other

    owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.

    retain

    -it says “keep this in the heap until I don’t point to it anymore”

    -in other words ” I’am the owner, you cannot dealloc this before aim fine with that ”

    -By default all instance variables and local variables are retain pointers.

    -We generally use retain for UIViewControllers (UI item’s parents)

    copy

    -copy is required when the object is mutable.

    -copy specifies the new value should be sent -copy on assignment and the old value sent -release.

    -copy is like retain returns an object which you must explicitly release (e.g., in dealloc) in non-garbage collected environments.

    -if you use copy then you still need to release that in dealloc.

    -Use this if you need the value of the object as it is at this moment, and you don’t want that value to reflect any changes made by other

    owners of the object. You will need to release the object when you are finished with it because you are retaining the copy.

    retain

    -it says “keep this in the heap until I don’t point to it anymore”

    -in other words ” I’am the owner, you cannot dealloc this before aim fine with that ”

    -By default all instance variables and local variables are retain pointers.

    -We generally use retain for UIViewControllers (UI item’s parents)