Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Chapter 1:

If your application uses too much memory, your devices operating system may abruptly terminate your application to prevent a system crash.

You can only read or write to directories that are part of your applications bundle or your applications documents directory. Areas accessible to your application are said to be in your applications sandbox. You cannot read files created by other applications unless the application places the files in its documents folder and explicitly indicates to iOS that it wishes to share its documents directory.

iOS, with its limited resources, does not include Objective-C 2.0 garbage collection, and you must manage memory yourself. Each class in objective C has .h [interface]file to declare the methods and properties and .m file [implementation]

Chapter 2:
Function call should be between square brackets as NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; #define MYNUMBER 20 can be added to header file to declare constant value C has no public or private functions. Static function in C is very different from a static function in Java. In C, declaring a function static is similar to declaring a function private in Java.

Chapter 3:
@interface directive is used to declare that this is the interface for the class while @implementation declares the implementation Implementation sample:

#import "Simple.h" @implementation Simple @end

Interface sample:

#import <Foundation/Foundation.h> @interface Simple : NSObject { } @end

Method declaration
-(void) sayHello: (NSString*) name;

Method implementation

-(void) sayHello: (NSString*) name { NSMutableString *message = [[NSMutableString alloc] initWithString:@"Hello there "]; [message appendString:name]; NSLog(message); [message release]; }

You use the compiler directives @private, @protected, and @public to declare instance variable visibility Note that you dont say that you call a method when using Objective-C. Instead, you send a message to a receiver. For instance, using Java you might type the following: objMyObject.getFooUsingID(33); When describing this line, I write that I am calling objMyObjects getFoo method and passing the argument 33. In Objective-C, the same message appears as follows: [objMyObject getFooUsingID : 33]; When describing this line, I write that I am sending a getFooUsingID message, passing the argument 33, and objMyObject is the receiver. Method name in Objective C is as follow:

The term self refers to an object when sending a message and it is also the receiver.

-(void) goHome { Milk * myMilk = [self pickupMilk]; } -(Milk*) pickupMilk { // pick up milk logic }

Nested Arguments , In java It will be like objMyObject.getFoo(objMyFooIdentifier.getID()); In objective C it will be like [objMyObject getFoo: [objMyFooIdentifier getID]]; Another example in java will be : objTester.testFubar(new Fubar(33)); In objective C it will be like [objTester testFubar[[Fubar alloc] initWithInteger : 33]]]; There are two method types: instance and class methods. Instance methods begin with a minus sign, while class methods begin with a plus sign. A class method is similar to a Java static method, meaning you dont need to create a class instance to use the method. Objective-C class methods have the same restrictions as Java static methods. You cant reference that class instance variables from a static method The alloc method is how you create class instances for all Objective-C classes. This method allocates memory space for the new object instance The init method is how you initialize a class once allocated. Unlike the class method alloc, the init method is an instance method. The init method returns an id. An id is an Objective-C type that is a pointer to the object instances address. The id is weakly typed, though, and the runtime treats all ids the same. Object life cycle
Simple *objSimple = [[Simple alloc] init]; [objSimple sayHello:@"James"]; [objSimple release];

When an objects reference count reaches zero, the runtime calls the objects dealloc method to deallocate the object.

Youve already seen how alloc, release, and dealloc work; you allocate memory for an object and assign it a reference count of one using the alloc method, and you decrement the reference count by one when calling release. When an objects reference count reaches zero,the program calls NSObjects dealloc method. The retain method increments an objects reference by one and returns the object reference as an id. Unlike Java, this referencing isnt automatic; you must explicitly call retain to increase an objects reference count.

Retain Sample:
Simple *objSimple = [[Simple alloc] init]; Simple *objSimpleTwo = objSimple; NSLog(@"retaincount: %d", [objSimple retainCount]); [objSimple release]; //the next line causes an error because objSimpleTwo is released [objSimpleTwo sayHello:@"James"];

Auto release can take place using:


Simple *objSimple = [[[Simple alloc] init] autorelease];

Chapter 4:
Managing memory when getting and setting an objects instance variables is a pain. Objective-C 2.0 makes instance variables easier by using properties. Properties are shortcuts for creating instance variable accessors. You create properties using compiler directives. The @property directive declares a property, @synthesize tells the compiler to generate accessors, and @dynamic tells the compiler you will provide the accessor methods. A property directive also has one or more attributes. The readonly attribute indicates the property is read-only, and the synthesize directive only creates a getter for the property. Retain instructs the compiler to create the setter so that it retains the object.

When setting this property, you must release whatever temporary instance you might create.
@property(retain) Simple objSimpleRetain; -(IBAction)giveWelcome { Simple* temp1 = [Simple alloc]; self.objSimpleRetain = temp1; [objSimpleRetain sayHello:@"Mike"]; NSLog(@"retaincount (mike): %d", [objSimpleRetain retainCount]); [temp1 release]; }

The temp1 reference is released at the methods end. If you ran the giveWelcome method, NSLog would print two as the retain count. The retain count of two is because the method first allocates temp1, which

sets the objects retain count to one, and then the method sets the property; because the property specified retain, the objects retain count becomes two. Finally, the method releases temp1 and the retain count returns to one. Assign will work as follow
@property(assign) Simple objSimple;

Specifying assign is equivalent to simply assigning a pointer to an object without increasing its retain count Where assign is appropriate is for creating primitive properties. For instance, you might make an integer a property of a class. An integer is a primitive, and so you assign values to the property; you do not assign a pointer to the integer.
@property (assign) int myInteger;

Copy will work as follow, Sometimes you might wish to obtain an independent object copy. You accomplish this using the copy attribute. When you use the copy attribute, the setter creates a new object and the original object is duplicated. This property is an independent object, not related to the original. There are two copy types: shallow and deep. To copy your own custom class, your class must implement the NSCopying protocol.
-(IBAction)giveWelcome { Foo * myFoo = [[Foo alloc] autorelease]; NSMutableString* message = [[[NSMutableString alloc] initWithString: @"A copied string."] autorelease]; myFoo.myString = message; [message appendString:@" More added to end of string."]; NSLog(myFoo.myString); }

In fact, to avoid memory leaks, remember this one rule: when using properties with the attributes nonatomic and retain, always release the properties in a dealloc method.
-(void) dealloc { [myString release]; [super dealloc]; }

Multiple arguments function in Java is like objMyClass.startPlay("Adventures of Tom Thumb", 44,new CherryPie( ), "Jack Sprat", 77); In objective C [objMyClass startPlay: @"Adventures of Tom Thumb" audienceMembers:44 pie: [[CherryPie alloc] init] supportingActor:@"Jack Sprat" extrasNeeded:77]; The method signature is -(void) startPlay: (NSString*) title audienceMembers: (int) value pie: (CherryPie*) pievalue supportingActor: (NSString*) actorvalue extrasNeeded: (int) extrasvalue;

The first argument is unnamed. The second and any further arguments are distinguished by a space followed by an argument name and colon, followed by the type in parentheses, followed by a parameter name to hold the value. When referring to a multiple-argument method, when calling the method, you refer to its named arguments. An arguments named argument is the name prior to the arguments data type. When using the argument within the methods implementation that the argument is a part of, you refer to the actual parameter name, not the argument name.

The id variable is a data type that represents an objects address. Because its just an address, id can be any object, and because its type is a pointer, you dont need to include the * symbol, as the * symbol signifies a pointer to a specific type. For instance, Foo * myFoo; Is a pointer to a Foo object. The compiler knows the pointer points to an address that contains an object of type Foo. However, the following, id myFoo; provides no such information to the compiler. The compiler only knows that myFoo is a pointerthe compiler knows where the pointer is pointing, but it doesnt know the data type of what myFoo points to. Only at runtime can it be determined what myFoo actually points to. Inheritance is as follow @interface SimpleChild : Simple Objective-C inheritance allows overriding methods, but not instance variables. You already saw an example of overriding methods when you overrode NSObjects dealloc, retain, and release methods in the class Foo. Unlike Java, you cannot overload methods when using Objective-C. In Java you overload a method when you provide a method with the same name but a different signature in the same class. For instance, you could define two methods like the following. The two methods are treated as distinct methods by the Java runtime
public void myMethod(String name); public void myMethod(int age);

Not so when using Objective-Cwhen faced with two methods like these next ones, the compiler issues an error and will not compile your class. -(void) myMethod: (NSString *) name; -(void) myMethod: (int) age; Because Objective-C does not support overloading, in the Objective-C programming community, it is common practice to add the arguments name to the method name. -(void) myMethodString: (NSString *) name; -(void) myMethodInt: (int) age; Finally, you should note that for multiple argument methods, Objective-Cs lack of method overloading is not problematic if any argument other than the first is different. Remember, a methods name includes its argument names. The following two method names are not the same: -(void) myMethod: (int) age name: (NSString *) theName; -(void) myMethod: (NSString *) name age: (int) theAge; Protocols are similar to Java interfaces. In Java, an interface specifies the methods that a class that implements the interface must have. This is often called a contract. A class claiming to implement an interface should implement that contractthe class is promising it contains implementations for all of the interfaces method declarations. So when you write a method like the following, you can rest assured the object has the called method implementation.

-(void) handleDoIt : (id <DoerProtocol>) objADoerImpl { [objADoerImpl doSomething]; }

MESH FAHMA protocols wala categories bs they said If you understood half of this chapters topics, you should do okay for the remainder of the book. @try { } @catch(NSException *e) { } @finally { }

Chapter 11:
When you create a table ,you have a choice of two styles: UITableViewStylePlain UITableViewStyleGrouped UITableView classes have an associated UITableViewController, a UITableViewDelegate, and a UITableViewDataSource. The UITableViewController is the controller class for a table view. You create an instance of this class to manage the UITableView. The UITableViewDelegate is a protocol you adopt in a custom class you write. This protocol allows you to manage selections, configure headers and footers, and manage cells. The UITableViewDataSource is also a protocol you adopt in a custom class. This protocol allows you to manage a tables data source. The UITableViewDelegate and UITableViewDataSource are protocols at least one class in your application must adopt if your application contains a UITableView. You can create your own custom classes to adopt these protocols, or create a UITableViewController that automatically adopts these protocols. If you choose to use a UITableViewController rather than custom classes, you simply connect the table views dataSource and delegate outlets to the UITableViewController. You can then implement both protocols methods in the UITableViewController.

Chapter 13:
Date pickers pick dates and times, placing the selected values in an NSDate class. You then create an IBOutlet for the picker in your views associated view controller and connect it to the picker on your views canvas. The view controller uses this outlet to obtain the UIDatePicker objects selected date. UIDatePickers return values as an NSDate. You access a date pickers value through its date property
NSDate * theDate = self.datePicker.date;

An NSDate is a date and time. Several NSDate methods you might use include isEqualToDate:, earlierDate:, compare:, and laterDate An NSDateFormatter allows a date to be displayed as a formatted NSString. There are many formats you might use. For instance, consider the two formatters.
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"MM/dd/yyyy"];

You might also like