AppDelegate Life Cycle:
View Controller Life cycle:
Difference between Delegates & Notifications:
Short Answer: You can think of delegates like a telephone call. You call up your buddy and specifically want to talk to them. You can say something, and they can respond. You can talk until you hang up the phone. Delegates, in much the same way, create a link between two objects, and you don't need to know what type the delegate will be, it simply has to implement the protocol. On the other hand, NSNotifications are like a radio station. They broadcast their message to whoever is willing to listen. The radio station can't receive feedback from it's listeners (unless it has a telephone, or delegate). The listeners can ignore the message, or they can do something with it. NSNotifications allow you to send a message to any objects, but you won't have a link between them to communicate back and forth. If you need this communication, you should probably implement a delegate. Otherwise, NSNotifications are simpler and easier to use, but may get you into trouble.
Long Answer:
Delegates are usually a more appropriate way of handling things, especially if you're creating a framework for others to use. You gain compile time checking for required methods when you use protocols with your delegates, so you know when you compile if you're missing any required methods. With NSNotificationCenter you have no such guarantee.
NSNotificationCenter is kind of "hack-ish" and is frequently used novice programmers which canlead to poor architecture. A lot of times these two features are interchangeable, but more "hardcore" developers might scoff at the use of the NSNotificationCenter.
|
Difference between Delegates & Protocols:
A protocol, declared with the (
@protocol
syntax in Objective-C) is used to declare a set of methods that a class "adopts" (declares that it will use this protocol) will implement. This means that you can specify in your code that, "you don't care which class is used as long as it implements a particular protocol". This can be done in Objective-C as follows:id<MyProtocol> instanceOfClassThatImplementsMyProtocol;
If you state this in your code, then any class that "conforms" to the protocol MyProtocol can be used in the variable instanceOfClassThatImplementsMyProtocol. This means that the code that uses this variable knows that it can use whichever methods are defined in MyProtocol with this particular variable, regardless of what class it is. This is a great way of avoiding the inheritance design pattern, and avoids tight coupling.
Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol. This particular protocol specifies methods that the delegate class should implement to perform specific actions at given events. The class that uses the delegate knows that its delegate coforms to the protocol, so it knows that it can call the implemented methods at given times. This design pattern is a great way of decoupling the classes, because it makes it really easy to exchange one delegate instance for another - all the programmer has to do is ensure that the replacement instance or class conforms to the necessary protocol (i.e. it implements the methods specified in the protocol)!
Protocols and delegates are not restricted only to Objective-C and Mac/iOS development, but the Objective-C language and the Apple frameworks make heavy use of this awesome language feature and design pattern.
Properties:
- A strong property is one where you increment the reference count of the object. If object A has a strong reference to B, and no other object is referencing B, B has count 1 (A owns, or needs to exist B). Now, if B wants to have a reference to A, we would want to use a weak reference. Weak references don't increment the reference count of the object. So in this particular case, if A has no other objects referencing it but B, A's count would be 0 given B's weak reference.
- Nonatomic is used to denote that the object being referenced in not thread safe. This means that the object is not able to deal with multiple requests at the same time.Atomicity is the idea that once you make a request, it either happens or it doesn't. When an operation is atomic, you're guaranteeing that the entity you're applying the operation to will never be in an intermediate state.
What are SDK tools in iPhone?
- Xcode
- IB
- Simulator
- Instruments
What is Xcode?
Xcode is an Integrated Development Environment IDE, it acts like an
Editor of Objective c Lang, We can write, run, build our code in
Xcode.
What is the simulator?
It is a demo to check our applications if we didn't have the original
device
What are Instruments?
The instrument enables you to dynamically trace and profile the
performance of the Mac os, iPhone, and iPad applications. Bu using
Instruments we can test the performance of the application.
What is Interface Builder?
Interface builder is developed in 1988.It is a visual tool that
allows you to design your interfaces for iPhone. By using interface
builder we can drag and drop the views on interface.
Explain about Inspector
It is mainly used for setting the properties of view elements. It
contains four sections.
1) Attribute inspector
2) Connection
3) Size of inspector
4) Identity
Groups and File sections:
Classes:
In class folder actual coding will be there. For window-based
application we have two files.
1) appdelegate.h
2) appdelegate.m
For view-based applications we have four files.
1) appdelegate.h
2) appdelegate.m
3) viewcontrol.h
4) viewcontrol.m
What is optimization?
Optimization is nothing but making our application more attractively
even after compilation of the coding
How can we use multiple class inheritance in
objective c?
In Objective c multiple inheritance is not supported for this purpose
we use "categories" in Objective c
How can u use object for abstract class?
(Or) What is Shared Obj?
We can’t create object for abstract class to call abstract
class we use Shared obj
What are the types of accessory methods?
We have 2 types of accessory methods:
I. Getter method:
Getter method is an accessory method that retrieves the value of an
instance variable.
II. Setter method:
Setter method is an accessory method that Sets the value of an
instance variable.
What is a Selector?
Selector can either be a name of method or message to an object.
What is Delegate?
- 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.
What is an App Delegate?
- It is necessary in any app.
- It is a controller, which takes care critical events while Starting Running and Editing applications
IPhone Paths:
IPhone contains two paths. They are,
1.Resource path: By using this path we can
retrieve the data from resource path.
But we cannot add the data.
2.Modify path: By using this path we can add, modify,
delete, retrieve the data.
Accelerometer:
The accelerometer allows the device to detect the orientation of the
device and adapts the content to suit the new orientation.
Header file:
Header files are preprocessor directories. In Obj c By using
#include, #import we can import the header files.
Resources:
In resources we can add the outside contents like images, videos,
audio files. It contains info.plist file.plist file having entire
project information.
Products:
The final output present in product folder and extension is .app.
Initially .app is in red color. Once if you can build the app
then the .app is created.
Files owner window:
It contains copy of nib files. It is always 1st icon in any .nib
file.
What is Enum?
It is a user defined data type.it improves speed of execution. By
default it assigns range from 0,1…
Interface:
In interface section that declares the methods and instance variables
of class.
Implementation:
In implementation section that actually defines the
class.
What kind of design pattern we are
using?
We are using model view controller
- Model: This layer manipulates the coding part of the application.
- View: This layer manipulates the design part of the application.
- Controller: This layer acts as bridge between model and view controller.
What is Id?
It is a datatype. Id is a generic c-type used by obj-c to refer to
any object.
NSFile manager:
By using NSFile manager we can compare, add, delete and update the
data in database.
NSBundle mainbundle:
It is used to pick the data from resources.
UI application:
Every iPhone has UI application.it is the starting point of the every
application. It is responsible for initializing and display your
application on UI Window and also responsible for loading your
application.
Controllers in iPhone:
By using controllers we can move from one view to another view.
Ex:pushviewcontroller,popviewcontroller,presentmodelviewcontroller,Dismissmodelviewcontroller.
How can we deploy our app?
By using standard edition worth’s 99$ we can deploy our app
How can we localize our app?
Localization have to use current language. For exam to make a delete
button
In English: Delete
In Japanese lan: Aurem
In Spanish: Delat
In this way we can localize our app
Why we use Authentication?
For the security purpose we use authentication
When keyboard appears, hides some part of UI
for that what shall we do?
Keyboard will appear only in some views like "Textfield","Textviews"
etc. but not in all the views
For display the keyboard we have to use "becomeFirstResponder"..
For dismiss the keyboard we have to use "resignFirstResponder"
Ex:
[textField becomeFirstResponder];
[textField resignFirstResponder];
What is awakeFromNib?
Activity should be allocated from loading xib file; it should be not
nil in awakFromNib And awakeFromNib is called when all file owners
outlets and properties are set (including view property). So it makes
sense that viewDidLoad is called earlier than awakeFromNib.
What is becomeFirstResponder?
For display the keyboard we have to use "becomeFirstResponder"..
Ex:[textField becomeFirstResponder];
What happens when we invoke a method on a
nil pointer?
It returns 0, nil, a structure filled with 0s,
Multiple class inheritance in objective c?
In Objective c multiple inheritance is not supported for this purpose
we use "categories" in Objective c
Categories:
A category allows you to add methods to an existing class. Categories
are a powerful feature that allows you to extend the functionality of
existing classes without subclassing.
The declaration of a category interface looks very much like a class
interface declaration except the category name is listed within
parentheses after the class name and the superclass aren’t
mentioned.
Extensions:
Class extensions are like anonymous categories, Except that the
methods they declare must be implemented in the main @implementation
block for the corresponding class.
Extensions allow you to declare additional required methods for a
class in locations other than within the primary class @interface
block,
What is XML parsing?
Xml parsing is the transfer of data from xml page to our application
the data which is in term of elements
What are the Delegate Methods for XML
Parsing?
There are mainly 3Tyoes of Delegate methods for XML Parsing
i. did start element
ii. did end element
iii. found characters
What is Picker view?
Picker view lets the user select an item from a list.
What is Table view?
It is commonly used to show list of data in the form of Records
Web Services?
Web services are application-programing interfaces [API] or Web API's
that are acceded via Hyper Text protocol and excited on a remote
system hosting the requested services
Differences between HTML & XML?
- XML was designed to transfer and store the data
- XML was to carry data not to display the data
- XML is designed to be self-description.
- HTML was designed to Display the data
Singleton:
A system only needs to create one instance of a class, and that
instance will be accessed throughout the program. Examples would
include objects needed for logging, communication, database access,
etc. You could pass such an instance from method to method, or assign
it to each object in the system. However, this adds a lot of
unnecessary complexity.
What is the difference Between APPKit and
UIKit?
There is NO APPKit in iphone and there is no UIKit in Mac OS
Deployment steps?
Deployment:
1) Open applications- utilities-keychain access –certificate
assistance – requested a certificate from a certificate
authority.
Email:
Savetodisk (radio button)
Ok
Certificate icon is created in desktop. (Developer certificate is
created)
2) Upload the developer certificate into developer.apple.com
Registered login:
3) Create the provisioning profile from developer.apple.com and
download it to system.
4) Open the Xcode and select the window -> organizer ->
select the device and under provisioning profile tab drag it to
device.
OBJECTIVE-C
This section provides you the basic introduction about Objective C
programming language. Objective C is very old programming language
and it was designed and developed in 1980. Now Objective C has become
popular once again as Apple is using it to developing applications
for Mac system and iPhone.
Brad Cox designed objective-C in his company Stepstone Corporation in
early 1980's. The Objective-C language is designed to enable a easier
and powerful object-oriented programming. It works as a powerful set
of extensions to the C language. Objective C takes best features from
C and Smalltalk. Objective C is easy to learn and has full object
oriented capabilities.
Objective C is simple and very intuitive programming language that
makes the Object Oriented programming sample and sophisticated.
Objective C is simple and small but it is a very powerful extension
of standard ANSI C language. Objective C provides full object
oriented programming capabilities just like C and all these things
are done in very simple and straightforward way.
Most of the programming language provides:
- A library of Objects
- Necessary development tools
- OOP' support and related libraries
Objective C provides all the above components. You can use Objective
C to develop full fledge applications. Apple has selected Objective C
as primary programming language for Mac machine and iPhone. So, you
can use Objective C to develop applications for these devices.
Like an object-oriented language Objective C revolves around objects.
It has three parts:
1. Inter-face Interface of a class is generally defined in
header file suffixed .h. It is a declaration of a class.
2. Implementation Actual code is written in
implementation of a class is generally defined in file of suffixed
.m. It is a definition of a class.
3. Instantiation After declaring and defining class we
can be instantiated by allocating memory to the new object of the
class.
It has a lot of features to make a powerful and object oriented
program in an easier way. Some are listed below:
1. It is a powerful language,
2. Easy-to-learn,
3. Object-oriented version of C,
4. Provide dynamic binding,
5. Run-time type identification, and persistence
6. Easy to understand code
7. Well organized language
Objective-C language is selected for the
Cocoa framework?
It has several reasons.
1. It is an Object oriented language and functionalities
provided by Cocoa framework can only be delivered by object-oriented
techniques.
2. It is an extension of ANSI C so the existing C program can be
use with the framework without loosing any work and user can take
benefit of C language.
With this language user can select both object oriented and
procedural language as per need.
3. It is simple and easy to learn because its syntax is small so
it can make a proficient programmer with much less difficult.
4. It is very dynamic as compared to other languages based on C.
Compiler play a very important role to preserve dealing of
information about objects to be use
at the run time.
5. It is a powerful language because decisions that might be
made at compile time can be postponed until the program is running.
Objective-C has dynamism has two major
benefits:
1. It supports an open dynamic binding that creates a
simple architecture to interactive user interface.
2. It enables to development of sophisticated development tools.
An interface to the run time system gives a facility to access the
information about application at rum time this makes possible to
monitor objective-C application.
Objective-C keywords
Objective-C is a superset of C language, so program written in c and
C++ should compile as objective-c. It provides some additional
keywords, to avoid conflict with keywords in other language it uses @
at the beginning of keyword. These keywords are called Compiler
Directives.
Directives used to declare and define classes, categories and
protocols:
Directive
|
Definition
|
@interface
|
Used to declare
of class or interface.
|
@implementation
|
Used to define a
class or category.
|
@protocol
|
Used to declare a
formal protocol.
|
@end
|
Ends the
declaration, definition, category or protocol.
|
Directive used to specify the visibility of the instance. Default
is @protected.
Directive
|
Definition
|
@private
|
Limits the scope
of an instance variable to the class that declares it.
|
@protected
|
Limits instance
variable scope to declaring and inheriting classes.
|
@public
|
Removes
restrictions on the scope of instance variables.
|
Exception handling directives.
Directive
|
Definition
|
@try
|
Defines a block
within which exceptions can be thrown.
|
@throw
|
Throws an
exception object.
|
@catch
|
Catches an
exception thrown within the preceding @try block.
|
@finally
|
A block of code
that is executed whether exceptions were thrown or not in a @try
block.
|
Directive used for particular purpose.
Directive
|
Definition
|
@class
|
Declares the
names of classes defined elsewhere.
|
@selector(method_name)
|
It returns the
compiled selector that identifies method_name.
|
@protocol(protocol_name)
|
Returns the protocol_name protocol (an instance of the Protocol
class). (@protocol is also valid without (protocol_name) for
forward
declarations.)
|
@encode(type_spec)
|
Yields a
character string that encodes the type structure of type_spec.
|
@"string"
|
Defines a constant NSString object in the current module and
initializes the
object with the specified 7-bit ASCII-encoded string.
|
@"string1" @"string2" ...
@"stringN"
|
Defines a constant NSString object in the currentmodule. The
string
created is the result of concatenating the strings specified in
the two
directives.
|
@synchronized()
|
Defines a block of code that must be executed only by one thread
at a time.
|
Some keywords of Objective-C are not reserved outside. These are..
in
|
out
|
inout
|
bycopy
|
byref
|
oneway
|
Keyword for memory management in Objective-C
These are looking as keywords but in fact these are methods of root
class NSObject.
alloc
|
retain
|
release
|
autorelease
|
Some other keywords:
1.bool is a keyword used in objective-C but its value is here
YES or NO. In C and C++ it has value either TRUE or FALSE.
2. 'Super’ and 'self' can be treated as keywords but self
is a hidden parameter to each method and super gives the instructions
to the compiler that how to use self differently.
Preprocessor Directives
Directive
|
Definition
|
//
|
This is used to
comment a single line.
|
#import
|
Like C and C++ it
is used to include a file but it doesn't include more than once.
|
The preprocessor directives are special notations:
Message Expressions in Objective-C
Message expressions are enclosed in square brackets as given
below:[receiver message]
Comments
Like C and C++ comments // and /*- - - - */ are allowed.
For example:
- // comment.
- /* comment. Comment. */
Because of objective-C is the extension of ANSI-C and it follows an
object-oriented approach so provides classes and objects. The way to
declare and define classes and creation of object is little bit
different from C and C++.
To declare a new class objective-C uses @interface directive.
Declaration of a simple class: MyClass.h
#import"SuperClass.h"
#import<headerFile.h>
@interface ClassName:SuperClass {
variable declaration;
variable declaration;
}
method declaration;
method declaration;
@end
|
#import<Foundation/NSObject.h>
@interface MyClass:NSObject{
int a;
int b;
}
-(void) setvara : (int) x;
-(void) setvarb : (int) y;
-(int) add;
@end
|
Definition of declared class: MyClass.m
#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
-(void) setvara :(int) x{
a=x;
}
-(void) setvarb :(int) y{
b=y;
}
-(int) add{
return a+b;
}
@end
|
Piecing it together
main.m
#import<stdio.h>
#import"MyClass.m"
int main(){
MyClass *class = [[MyClass alloc]init];
[class setvara : 5];
[class setvarb : 6];
printf("Sum is : %d",[class add]);
[class release];
return ;
}
|
Objective-C enables programmer to use method with multiple parameter.
These parameter can be of same type or of different type.
This is a sample program that shows sum of three numbers as output.
MyClass.h
#import<Foundation/NSObject.h>
@interface MyClass:NSObject{
}
// declare method for more than one parameter
-(int) sum: (int) a andb: (int) b andc:(int)c;
@end
|
MyClass.m//Syntax
#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
-(int) sum: (int) a andb: (int) b andc:(int)c;{
return a+b+c;
}
@end
|
MyClass.m
#import<stdio.h>
#import"MyClass.m"
int main(){
MyClass *class = [[MyClass alloc]init];
printf("Sum is : %d",[class sum : 5 andb : 6 andc:10]);
[class release];
return ;
}
|
Output:
Sum is : 21
|
Constructors
Objective-C enables user to define constructor with the help of self
and super keywords. Like java Objective-C has parent class and
programmer can access its constructor by statement [super init], this
statement returns a instance of parent class which we assign to the
'self' keyword, actually 'self' plays same role as this keyword in
C++ and Java. The default constructor is -(id) init statement
if(self) is used to check the condition self != nil to confirm that
parent class returned a new object successfully.
Example:
MyClass.h
#import<Foundation/NSObject.h>
@interface MyClass:NSObject{
int a;
int b;
}
// declare constructor
-(MyClass*) set:(int) a andb:(int) b;
-(void) sum;
@end
|
MyClass.m
#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
// define constructor
-(MyClass*) set:(int) x andb:(int) y {
self = [super init];
if(self) {
a=x;
b=y;
return self;
}
}
-(void) sum {
printf("Sum is : %d",a+b);
}
@end
|
MyClassMain.m
#import<stdio.h>
#import"MyClass.m"
int main(){
// use constructor
MyClass *class = [[MyClass alloc] set : 10 andb : 12];
[class sum];
[class release];
return ;
}
|
Output:
Sum is : 22
|
Previously it was a requirement to allocate and release memory
manually to assist with this problem it provides a reference-counting
memory management system through retain and release keywords. But it
is still required to take care of memory management by the
programmer.
Going one step further in version 2.0 garbage collectors is
implemented as a conservative collector. This enables users to use
full functionality of C as well as preserves Objective-C's ability to
integrate with C++ code and libraries.
Access Privileges
1. Default access in objective-C is @protected.
2. Like C++ objective-C provide public and private access
modifiers as well.
3. @protected accessifier enable access elements in the
subclass.
Example:
MyClass.h
#import<Foundation/NSObject.h>
@interface MyClass:NSObject {
@private
int a;
int b;
}
-(void) set:(int) x andb:(int) y;
-(void) sum;
-(void)show;
@end
|
MyClass.m
#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
-(void) set:(int) x andb:(int) y {
a=x;
b=y;
}
-(void) sum {
printf("Sum is : %d \n",a+b);
}
-(void)show{
printf("value of a is : %d \n",a);
printf("value of b is : %d \n",b);
}
@end
|
MyClassMain.m
#import<stdio.h>
#import"MyClass.m"
int main(){
MyClass *class1 = [[MyClass alloc] init];
MyClass *class2 = [[MyClass alloc] init];
[class1 set: 10 andb :12];
[class1 show];
[class1 sum];
// This is invalid statement because variable a is private.
// class2->a = 10;
class2->b = 15;
[class2 show];
[class2 sum];
[class1 release];
[class1 release];
return ;
}
|
Output:
value of a is : 10
value of b is : 12
Sum is : 22
value of b is : 15
Sum is : 15
Class level access
Objective-C provides facility of class level access. In the examples
given above we have used '-' sign before method, '-' means instance
level access. Now we will see how to define method that can be access
on class level.
Here in this example we have created a method named 'classShow' that
can be accessed on class level means no need to create object to use
classShow() method. We can directly use this method through class
name. +(void)init method is called when objective-C program starts
and it calls for every class so it is the better place to define
class level variable.
Default constructor syntax is?
- (id) init.
What happens if you remove the object from
the array, and you try to use it?
Our application crashes
Difference Between nil & NIL?
Both nil, Nil return a NULL pointer (Zero pointer) but
- nil is specific to objects (e.g., id)
- Nil is specific to class pointers.
Ex:
- For the NULL pointers in Objective c We have to use: nil,
- In the same way for the NULL pointers in C, C++ We have to use: NIL
NIL is just like NULL