Objective C QP1
Objective C QP1
9- What is
dynamic
You use the @dynamic keyword to tell the compiler that you will
fulfill the API contract implied by a property either by providing
method implementations directly or at runtime using other
mechanisms such as dynamic loading of code or dynamic method
resolution. It suppresses the warnings that the compiler would
otherwise generate if it cant find suitable implementations. You
should use it only if you know that the methods will be available
at runtime
10-If I call performSelector:withObject:afterDelay: is the
object retained?
Yes, the object is retained. It creates a timer that calls a
selector on the current threads run loop. It may not be 100%
precise time-wise as it attempts to dequeue the message from
the run loop and perform the selector.
11-Can you explain what happens when you call autorelease
on an object?
When you send an object a autorelease message, its retain count
is decremented by 1 at some stage in the future. The object is
added to an autorelease pool on the current thread. The main
thread loop creates an autorelease pool at the beginning of the
function, and release it at the end. This establishes a pool for
the lifetime of the task. However, this also means that any
autoreleased objects created during the lifetime of the task are
not disposed of until the task completes. This may lead to the
tasks memory footprint increasing unnecessarily. You can also
consider creating pools with a narrower scope or use
NSOperationQueue with its own autorelease pool. (Also
important You only release or autorelease objects you own.)
12-Whats the NSCoder class used for?
Well you would want to implement the getter and setter for the title
object. Something like this: view source print?
- (NSString*) title // Getter method
{
return title;
}
- (void) setTitle: (NSString*) newTitle //Setter method
{
if (newTitle != title)
{
[title release];
title = [newTitle retain]; // Or copy, depending on your needs.
}
}
19-Implement the following methods: retain, release, autorelease.
-(id)retain
{
NSIncrementExtraRefCount(self);
return self;
}
-(void)release
{
if(NSDecrementExtraRefCountWasZero(self))
{
NSDeallocateObject(self);
}
}
-(id)autorelease
{ // Add the object to the autorelease pool
[NSAutoreleasePool addObject:self];
return self20-What are the App states. Explain them?
Not running State: The app has not been launched or was running but
was terminated by the system.
Inactive state: 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. The only time it stays inactive for any period of time is when
the user locks the screen or the system prompts the user to respond
to some event, such as an incoming phone call or SMS message.
Active state: The app is running in the foreground and is receiving
events. This is the normal mode for foreground apps.
Background state: 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
[_internallock];//lockusinganobjectlevellock
idresult=[[valueretain]autorelease];
[_internalunlock];
returnresult;
emp.age = @20;
Now how KVC works is like this
[emp valueForKey:@"age"];
[emp setValue:@"25" forKey:@"age"];
KVO : The mechanism through which objects are notified when there
is change in any of property is called KVO.
For example, person object is interested in getting notification when
accountBalance property is changed in BankAccount object.To achieve
this, Person Object must register as an observer of the BankAccounts
accountBalance property by sending an
addObserver:forKeyPath:options:context: message.
98-Can we use two tableview controllers on one view controller?
Yes, we can use two tableviews on the same view controllers and you
can differentiate between two by assigning them tagsor you can
also check them by comparing their memory addresses.
99-Swap the two variable values without taking third variable?
int x=10;int y=5;x=x+y;NSLog(@x==> %d,x);
y=x-y;NSLog(@Y Value==> %d,y);
x=x-y;NSLog(@x Value==> %d,x);
100-What is push notification?
Imagine, you are looking for a job. You go to software company daily
and ask sir is there any job for me and they keep on saying no. Your
time and money is wasted on each trip.(Pull Request mechanism)
So, one day owner says, if there is any suitable job for you, I will let
you know. In this mechanism, your time and money is not wasted.
(Push Mechanism)
How it works?
This service is provided by Apple in which rather than pinging server
after specific interval for data which is also called pull mechanism,
server will send notification to your device that there is new piece of
You must create your own autorelease pool as soon as the thread
begins executing; otherwise, your application will leak objects
104-Three occasions when you might use your own
autorelease pools:
1.
2.
3.
3.
4.
Thats it, pretty fast and easy, but there are a lot of caveats :
The most important problem is that the thread which called
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 doesnt have to constantly check whether or not a certain
method is implemented by the distant object.
121- Formal vs informal protocol.
In addition to formal protocols, you can also define an informal
protocol by grouping the methods in a category declaration:
@interface NSObject (MyProtocol)
//someMethod();
@end
Informal protocols are typically declared as categories of the
NSObject class, because that broadly associates the method names
with any class that inherits from NSObject. Because all classes inherit
from the root class, the methods arent restricted to any part of the
inheritance hierarchy. (It is also possible to declare an informal
protocol as a category of another class to limit it to a certain branch
of the inheritance hierarchy, but there is little reason to do so.)
When used to declare a protocol, a category interface doesnt have a
corresponding implementation. Instead, classes that implement the
protocol declare the methods again in their own interface files and
define them along with other methods in their implementation files.
An informal protocol bends the rules of category declarations to list a
group of methods but not associate them with any particular class or
implementation.
Being informal, protocols declared in categories dont receive much
language support. Theres no type checking at compile time nor a
check at runtime to see whether an object conforms to the protocol.
To get these benefits, you must use a formal protocol. An informal
protocol may be useful when all the methods are optional, such as
for a delegate, but (in Mac OS X v10.5 and later) it is typically better
to use a formal protocol with optional methods.
122- Optional vs required
Protocol methods can be marked as optional using the @optional
}
125- alloc vs new
alloc creates a new memory location but doesnt initializes it as
compared to new.
126- release vs pool drain
release frees a memory. drain releases the NSAutoreleasePool
itself.
127- NSAutoReleasePool : release vs drain
Strictly speaking, from the big picture perspective drain is not
equivalent to release:
In a reference-counted environment, drain does perform the same
operations as release, so the two are in that sense equivalent. To
emphasise, this means you do not leak a pool if you use drain rather
than release.
In a garbage-collected environment, release is a no-op. Thus it has no
effect. drain, on the other hand, contains a hint to the collector that
it should collect if needed. Thus in a garbage-collected
environment, using drain helps the system balance collection sweeps.
128-autorelease vs release
Autorelase: By sending an object an autorelease message, it is added
to the local AutoReleasePool, and you no longer have to worry about
it, because when the AutoReleasePool is destroyed (as happens in the
course of event processing by the system) the object will receive a
release message, its RetainCount will be decremented, and the
GarbageCollection system will destroy the object if the RetainCount
is zero.
Release: retain count is decremented at this point.
129- Autorelease Pool
Autorelease pools provide a mechanism whereby you can send an
object a deferred release message. This is useful in situations
where you want to relinquish ownership of an object, but want to
avoid the possibility of it being deallocated immediately (such as
when you return an object from a method). Typically, you dont need
to create your own autorelease pools, but there are some situations
in which either you must or it is beneficial to do so.
130- How autorelease pool is managed.
Every time -autorelease is sent to an object, it is added to the innermost autorelease pool. When the pool is drained, it simply sends
-release to all the objects in the pool.
Core Data