The PPTs from one of the event of iWillStudy.com - a leading start-up in the education space in India. This PPT is being used at an event where they taught iPhone programming and applications development.
7. Objective C Copyright
Objective C is simply a super set of c.
One can easily compile c code using Objective C
compiler and alternatively the developer can also
put c code any where in the objective c code.
Syntax of all non Object Oriented operations are
similar as C.
Rest syntax is taken from Smalltalk.
Everything Happens At Runtime( Every error is just a
warning).
9. Message Passing Copyright
C++ Way
(obj->methodName(Parameter))
Objective C Way
([obj methodName:Parameter])
Target of the message is always resolved at
runtime.
In Objective-C one does not call a method;
one sends a message
10. Interfaces 1 Copyright
@interface classname : superclassname
{
// instance variables The interface of a class is
} usually defined in a header
file
+ classMethod1;
+ (return_type)classMethod2;
+ (return_type)classMethod3:(param1_type)param1_varName;
- (return_type)instanceMethod1:(param1_type)param1_varName
andOtherParameter:(param2_type)param2_varName;
-
(return_type)instanceMethod2WithParameter:(param1_type)param1_
varName andOtherParameter:(param2_type)param2_varName;
@end
11. Interfaces Copyright
class classname : public superclassname
{
public:
// instance variables
The code above is roughly
equivalent to the following C++
// Class (static) functions
interface
static void * classMethod1();
static return_type classMethod2();
static return_type classMethod3(param1_type param1_varName);
// Instance (member) functions
return_type instanceMethod1(param1_type param1_varName, param2_type
param2_varName);
return_type instanceMethod2WithParameter(param1_type param1_varName,
param2_type param2_varName=default);
};
12. Implementation Copyright
Implementation (method) files normally have the file
extension .m, which originally signified "messages“
@implementationclassname
+classMethod {
// implementation
}
The interface only declares
-instanceMethod {
the class interface and not the
// implementation methods themselves: the actual
} code is written in the
@end implementation file
13. Instantiation Copyright
Object is instantiated by first allocating the memory
for a new object and then initializing.
Instantiation with default initializer
NSArray *array = [[NSArray alloc] init];
Or you can use
NSArray *array = [NSArray new];
Instantiation with Custom initializer
NSArray *array = [[NSArray alloc]
initWithObjects:@"String1",@"String2",nil];
14. Protocol Copyright
Polymorphism in c++.
Two Types:
Formal Protocol.
Informal Protocol.
Delegates are example of Informal Protocol.
Interface Classes from c++ are example of Formal
Protocol.
A formal protocol is similar to
An informal protocol is a list of
an interface in Java or C#. It is a list
methods that a class can opt to
of methods that any class can
implement.
declare itself to implement.
15. Managing Memory Copyright
Basic Fundamentals
Memory Management Rules
Passing objects between Methods
Working with Properties
Deallocating Object
Leaks and Static Analyzer
Rule #1 – If you create an object using alloc or copy, you
need to free that object.
Rule #2 – If you didn’t create an object directly, don’t
attempt to release the memory for the object.
16. Managing Memory:
Fundamentals Copyright
What is memory management?
“Managing the resources efficiently and effectively”.
Memory management in other languages.
In C,C++ we have malloc, calloc and free.
In other object oriented languages we have Garbage
Collectors.
So what is there in Objective C?
“Retain Count”
No need to worry about Primitive Data Types
17. Memory Management:
Rules Copyright
You own any object you create.
You “create” an object using a method whose name begins with “alloc”
or “new” or contains “copy”.
e.g. Alloc
MyClass *myObject = [[MyClass alloc] init];
// do your work with myObject
[myObject release] ;
e.g. Copy
18. Memory Management:
Rules Copyright
You can take ownership of an object using retain method.
NSArray* componentArray = [[self
cellforIndexPath:indexPath]retain];
/*
*/
[componentArray release];
You must relinquish ownership of objects you own when you’re finished
with them.
You relinquish ownership of an object by sending it a release
message or an autorelease message.
You must not relinquish ownership of an object you do not own.
NSString* myString = [NSString stringWithFormat:@"%@",[myItem currentItem]];
[myString release]; // this is absolutely wrong
19. Memory Management:
Passing Objects between Methods Copyright
This is wrong
– (NSArray *)sprockets {
NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket, auxiliarySprocket, nil];
return array;
}
This is also wrong
– (NSArray *)sprockets {
NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket,
auxiliarySprocket,nil];
[array release];
return array; // array is invalid here
}
20. Memory Management:
Passing Objects between Methods Copyright
The right way
– (NSArray *)sprockets {
NSArray *array = [NSArray arrayWithObjects:mainSprocket, auxiliarySprocket, nil];
return array;
}
You can also do this
– (NSArray *)sprockets {
NSArray *array = [[NSArray alloc] initWithObjects:mainSprocket, auxiliarySprocket, nil];
return [array autorelease];
}
21. Working with Properties Copyright
Property Declaration
@interface myClass:NSObject
{
float aValue;
}
@property float aValue;
@end
@property float aValue is equal to :
float aValue;
setValue(float iValue);
22. Working with Properties Copyright
Assign
No need to worry about Memory here. Just a pointer to
the object is assigned to variable.
Self.parentController = controller
Copy
self.controlArray = [someOtherArray copy];
//retain count controlArray = 1, someOtherArray = 1
[someOtherArray release];
//we need to release someOtherArray
23. Deallocating Objects Copyright
When object’s retain count drops to 0, its memory is
reclaimed in Cocoa terminology it is “freed” or
“deallocated.”
Os does this calling the “dealloc” method.
e.g. -(void)dealloc {
[myObject1 release];
[someContainer release];
[super release];
}
You should never invoke another object’s dealloc
method directly.
25. Installing App in iPhone Copyright
First Thing You need a Apple Developer Account.
Once you have a account you will have a developer
portal.
Using the portal you can generate provisioning files
for your device (which are nothing but certificates).
And you need to add that certificate information to
your application and then install that in the device.