1)
What is an Objective-C?
Objective-C
defines a small but powerful set of extensions to the ANSI C programming
language that enables sophisticated object-oriented programming. Objective-C is
the native language for Cocoa programming.
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
2)
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 variable 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.
Objective-C
vs C/C++.
o
The
Objective-C class allows a method and a variable with the exact same name. In
C++, they must be different.
o
Objective-C
does not have a constructor or destructor. Instead it has init and dealloc
methods, which must be called explicitly.
o
Objective-C
uses + and - to differentiate between factory and instance methods, C++ uses
static to specify a factory method.
o
Multiple
inheritance is not allowed in Obj-C, however we can use protocol to some
extent.
o
Obj-C
has runtime binding leading to dynamic linking.
o
Obj-C
has got categories.
o
Objective-C
has a work-around for method overloading, but none for operator overloading.
o
Objective-C
also does not allow stack based objects. Each object must be a pointer to a
block of
memory.
o
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.
o
One
of C++'s advantages and disadvantages is automatic type coercion.
o
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.
o
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.
3) What are the types
of accessory methods?
We have 2 types of accessor methods:
i.Getter method:
Getter
method is an accessor method that retrieves the value of an instance variable.
ii.Setter method:
Setter
method is an accessor method that Sets the value of an instance variable.
4) What is a Selector?
Selector can either be a name of method or message to an object.
Compiled selectors are of type SEL. There are two common ways to get a selector:
At compile time, you use the compiler
directive @selector.
SEL aSelector = @selector(methodName);
At runtime, you use the NSSelectorFromString function, where the string is the name of the method:
SEL aSelector = NSSelectorFromString(@"methodName");
Using a Selector
You can invoke a method using a selector with performSelector:
and other similar methods.
SEL aSelector = @selector(run);
[aDog performSelector:aSelector];
[anAthlete performSelector:aSelector];
[aComputerSimulation performSelector:aSelector];
5) What is Delegate?
It is a helper object.
it is an object that
usually read to some events in another object
A Delegate allows one object to send messages to another object
when an Event happens.
An object directly to carryout an action by another object.
6)
What are the App states. Explain them?
State
|
Description
|
Not running
|
The app has not been
launched or was running but was terminated by the system.
|
Inactive
|
The app is running in the foreground but is
currently not receiving events. (It may be executing other code though.) An
app usually stays in this state only briefly as it transitions to a different
state.
|
Active
|
The app is running in the
foreground and is receiving events. This is the normal mode for foreground
apps.
|
Background
|
The app is in the background and executing
code. Most apps enter this state briefly on their way to being suspended.
However, an app that requests extra execution time may remain in this state
for a period of time. In addition, an app being launched directly into the
background enters this state instead of the inactive state.
|
Suspended
|
The app is in the
background but is not executing code. The system moves apps to this state
automatically and does not notify them before doing so. While suspended, an
app remains in memory but does not execute any code.
When
a low-memory condition occurs, the system may purge suspended apps without
notice to make more space for the foreground app.
|
State changes in an iOS app
7)
What is Automatic Reference Counting (ARC)?
ARC
is a compiler-level feature that simplifies the process of managing the
lifetimes of Objective-C objects. Instead of you having to remember when to retain
or release an object, ARC evaluates the lifetime requirements of your objects
and automatically inserts the appropriate method calls at compile time.
8)
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]
9)
Immutable vs Mutable
Immutable
objects cant be changed. However they are just pointing to some location where
stored values are made constant. You can change that reference to other
location. Mutable objects can change the value.
10
) What is the difference between retain & assign?
Assign
creates a reference
from one object to another without increasing the source’s retain count.
if
(_variable != object)
{
[_variable release];
_variable = nil;
_variable = object;
}
Retain
creates a reference
from one object to another and increases the retain count of the source object.
if
(_variable != object)
{ [_variable release];
_variable = nil;
_variable = [object
retain];
}
11)
Why do we need to use @Synthesize?
We
can use generated code like nonatomic, atmoic, retain without writing any lines
of code. We also have getter and setter methods. To use this, you have 2 other
ways: @synthesize or @dynamic: @synthesize, compiler will generate the getter
and setter automatically for you, @dynamic: you have to write them
yourself.@property is really good for memory management, for example:
retain.How can you do retain without @property?
if
(_variable != object)
{
[_variable release];
_variable = nil;
_variable = [object
retain];
}
How
can you use it with @property?self.variable = object; When we are calling the
above line, we actually call the setter like [self setVariable:object] and then
the generated setter will do its job
12)
What is categories in iOS?
You 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.
@interface SystemClass (CategoryName)
// method declarations
@end
A common naming convention is that the base
file name of the category is the name of the class the category extends
followed by “+” followed by the name of the category. This category might be
declared in a file named SystemClass+CategoryName.h.
Implementation:
#import "SystemClass+CategoryName.h"
@implementation SystemClass ( CategoryName )
// method definitions
@end
13) What is Delegation in iOS?
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 thewindowWillResize:toSize:
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 theapplicationDidFinishLaunching: message. The delegate
is then able to perform additional tasks if it wants.
14)
How can we achieve singleton pattern in iOS?
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 NSFileManager, NSWorkspace, NSApplication, and, in UIKit,
UIApplication. 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.
15)
What is delegate pattern in iOS?
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 NSXMLParserobject) 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.Refer: delegate pattern
16)
What are all the difference between categories and subclasses?Why should we go
to subclasses?
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.


No comments:
Post a Comment