2009年2月25日 星期三

develop tool

omniobject meter: find memory leak

2009年2月18日 星期三

category

1. add new method to existing class
2. can not add new instance variable to existing class
3. if the method defined in category is the same as existing class,
    it will replace original method of existing class
ex:
add category NumberConvenience to NSString
@interface NSString (NumberConvenience) 
- (NSNumber *) lengthAsNumber; 
@end // NumberConvenience 

@implementation NSString (NumberConvenience) 
- (NSNumber *) lengthAsNumber 
  unsigned int length = [self length]; 
  return ([NSNumber numberWithUnsignedInt: length]); 
} // lengthAsNumber 
@end // NumberConvenience

4. splitting a class's implementation across multiple files or multiple frameworks
ex:
CategoryThing.h
#import  
@interface CategoryThing : NSObject { 
  int thing1; 
  int thing2; 
  int thing3; 
@end // CategoryThing 

@interface CategoryThing (Thing1) 
- (void) setThing1: (int) thing1; 
- (int) thing1; 
@end // CategoryThing (Thing1) 

@interface CategoryThing (Thing2) 
- (void) setThing2: (int) thing2; 
- (int) thing2; 
@end // CategoryThing (Thing2) 

@interface CategoryThing (Thing3) 
- (void) setThing3: (int) thing3; 
- (int) thing3; 
@end // CategoryThing (Thing3) 

categoryThing.m
#import "CategoryThing.h" 
@implementation CategoryThing 
- (NSString *) description 
  NSString *desc; 
  desc = [NSString stringWithFormat: @"%d %d %d", 
           thing1, thing2, thing3]; 
  return (desc); 
} // description 
@end // CategoryThing 

thing1.m
#import "CategoryThing.h" 
@implementation CategoryThing (Thing1) 
- (void) setThing1: (int) t1 
  thing1 = t1; 
} // setThing1 
 
- (int) thing1 
  return (thing1); 
} // thing1 
@end // CategoryThing 

thing2.m & thing3.m are like thing1.m

2009年2月16日 星期一

method calling

method calling:
ex:
[ c setName: @"peter" age: 3];
two parameters, first is @"peter", second is 3
age: is name for second argument

advantage: readability

2009年2月10日 星期二

xcode

tab: complete name

esc: show possible name

ctrl + / :  jump to next argument in the function

move cursor:
ctrl + F: forward
ctrl + B: backward
ctrl + P: to previous line
ctrl + N: to next line
ctrl + A: to beginning of the line
ctrl + E: to end of the line
ctrl + D: delete the char right to the cursor
ctrl + K: delete the chars after the cursor at the same line

cmd + shift + D: show open quickly window to jump to definition

cmd + D: add to bookmark

option + double click:  search in document

2009年2月9日 星期一

memory management

use retain count to do memory management

when retain count becomes 0, the object is destroyed(is sent dealloc message)

when object is created via alloc, new, or copy, the object's retain count is set to 1

use retain to increase retain count

use release to decrease retain count

use retainCount to know object's retain count

retain count in accessor:
ex:
class Boy has an instance variable for book
- (void) setBook: (Book*) newBook
{
[newBook retain];
[book release];
book = newBook;
}
note:
when boy object is deallocated, it will release book object's retain count for retain count it increase in setBook

autorelease:
when object is sent autorelease message, it is put in autorelease pool
when the pool is destroyed, all objects in pool are sent a release message
ex:
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
Book *book = [[Book alloc] init];
[book autorelease];
[pool release];
note:
1. for iphone, avoid using autorelease and the functions that give autoreleased object
2. the same object can be autoreleased many times

drain:
the drain method for pool empties out the pool without destroying the pool

rule for reatin count:
1. when getting object from new, alloc, or copy, you must send the object release or autorelease when you are done with it
2. when getting object via other methods, assume it has a retain count of 1 and it has been autoreleased . If you want to hold object for some time, just retain it and release it when you are done
ex:
in an event loop, a pool is created before handling event. And the pool is destroyed after handling event. Hence, the object created during the event loop must retain if you want to keep it
3. if your retain an object, you need to release or autorelease it

garbage collection:
not support iphone
enable gc:
Build -> objective-c garbage collection

dynamic and late binding

you don't have to know the object is class People before calling People's speak method,
the object will find speak method automatically

ex:
Shape *a = [Circle new];
[a draw];
note: Circle's draw method is called

In java, you have to change object's type to People to call People's method
ex:
Shape a = new Circle();
a.draw();  // Shape's draw method is called
(Circle)a.draw();   // Circle's draw method is called

create object ,new, alloc, init, dealloc

new:
ex:
id c = [Circle new];

when creating object with new, two steps are performed :
(1) allocate memory for object
(2) object's init method is called
ex: 
- (id) init
{
self = [super init];
return self;
}

alloc:
initialize all memory to 0
Hence, all instance variable have zero value
pointer to objects are nil
integer variable is 0

dealloc:
- (void) dealloc