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
 

class:interface, implementation, inheritance, accessor, @property, dot

class declaration:
ex:
@interface Student : NSObject
{
int age;
}
- (void) setAge: (int) age;
- (void) speak;
@end

note:
1.if a method has argument, its name has a colon
2. inheritance:
a. use : , NSObject is super class of Student
b. not support multiple inheritance
3. isa:
instance varabile of NSObject, a pointer to the object's class
ex:
Shape *s = [Circle new];
NSLog(@"isa %@", s->isa);
---> Circle
4: super
call parent class's method definition
ex:
[super speak];

class definition:
@implementation Student
- (void) setAge: (int) a
{
age = a;
}

- (void) speak
{
NSLog(@"speak");
}

@end

note: if parameter uses the same name as instance variable,
use self to distinguish instance variable and parameter
ex:
self->name & name

accessor:
instance variable is protected, can access directly, but usually access from method
ex:
Circle *a = [Circle new];
NSLog(@"name %s", a->name);
id b = [Circle new];
NSLog(@"name %s", ((Circle*)b)->name);
note:
if the variable is with type of parent class,
it will not know instance variable defined in child class
Hence, must use cast
However, call method does not need to cast
the object will find suitable method automatically

name convention for accessor:
getter name conventions:
the same as instance variable name
setter name conventions:
add set before instance variable name
ex:
setAge

new accessor method: @property & @synthesize
create accessor for you
ex:
@interface People {
int age;
}
@property int age;
@end

@implementation People
@synthesize age;
@end

dot:
you can use dot to access instance variable
ex:
peter.age


special keyword in objective-c, id, BOOL, nil, @class

id:
a generic type to refer to any kind of object
ex:
id c = [Circle new];

BOOL:
type for boolean
YES(1) or NO(0)

nil:
1. a zero value
2. a message to nil does not crash, it just do nothing

@class:
when you only need to know the name of class, does not need to know internal structure of class , you can use @class keyword instead of including class header file
ex:
when @class Boy has a instance variable to object dog
use @class Dog
not use #import Dog.h

2009年2月4日 星期三

hello world example, NSLog, NSString

1. new project --> command line utility --> foundation tool

2. Hello.m
#import
int main (int argc, const char *argv[])
{
NSLog (@"Hello, Objective- C!");
return (0);
} // main

3. build and go
click build and go button or com+ R

4. show console
selelct Console from Run or cmd+shift+R


note:
1. import:
like include in C, but guarantees that a header file will be
included only once, no matter how many times the #import
directive is actually seen for
that file.

2. foundation framework:
handle features found in the layers beneath UI, such as
data structure and communication mechanisms
location:
/System/Library/Frameworks/Foundation.framework/Headers/

3. master header file
Each framework has a master header file that includes all the
framework’s individual header files. By using #import on the master header file,
you have access to all the framework’s features

4. NSLog
like printf, but also print date, time , and append newline
%@:
to print object, the object's description method defines what to print
ex:
- (NSString *) description 
    return (@"I am a hero"); 
}

5. NSString
begin with @, such as @"hello"
NSString's advantage:
(1) tell you length
(2)compare with another
(3)convert to interger or floating point