use @"abc" to mean NSString
ex: NSString *str = @"Hello";
use content of file to create NSString
ex: NSString *str = [NSString stringWithContentsOfFile:@"/path/to/file"]
use c characters to create NSString
ex: char *cStr="hello";
NSString *str = [NSString stringWithCString: cStr]
get length of NSString
ex: unsigned int strLen = [str length]
append on NSString to another
ex: NSString *str = @"Hello";
NSString *str2 = [str stringByAppendingString: @"abc"]
append a format:
ex: NSString *str3 = [str2 stringByAppendingFormat: @"%d", 2003]
search for subString:
ex: NSRange loc = [str rangeOfString:@"The"]
what is NSRange:
typedef struct _NSRange{
unsigned int location;
unsigned int length;
}NSRange;
breaking a string into components:
ex: NSArray *fields = [str componentsSeperatedByString:@"abc"];
create NSMutableString from NSString:
ex: NSString *str = @"hello";
NSMutableString *ms = [NSMutableString stringWithString: str];
2008年2月29日 星期五
mutable and immutable
immutable class means you can not alter after initialization
ex: NSString, NSArray
mutable class means you can alter after initialization, it extends from immutable class
ex: NSMutableString, MSMutableArray
ex: NSString, NSArray
mutable class means you can alter after initialization, it extends from immutable class
ex: NSMutableString, MSMutableArray
2008年2月28日 星期四
ButtonGroup
同一個group裡的button,只有一個會被選。
how to use:
1. create ButtonGroup object
2. add buttons to ButtonGroup object
how to use:
1. create ButtonGroup object
2. add buttons to ButtonGroup object
目前時間 in java
利用Calendar class
java.util.Calendar
ex: 顯示目前幾點
Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
顯示目前幾分
Calendar.getInstance().get(Calendar.MINUTE)
java.util.Calendar
ex: 顯示目前幾點
Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
顯示目前幾分
Calendar.getInstance().get(Calendar.MINUTE)
icon composer
create software's icon
/Developer/Applications/Utilities
how to set application's icon:
modify info.plist
ex:
CFBundleIconFile
APPL
/Developer/Applications/Utilities
how to set application's icon:
modify info.plist
ex:
xcode 指令
在function名稱上,option+double click:
show document to explain the function
在function名稱上,apple + double click:
show the function's header file
show document to explain the function
在function名稱上,apple + double click:
show the function's header file
2008年2月27日 星期三
mvc
mvc( model, view, controller)
model: 實際做的function,例如1加到100
view: user看到的GUI
controller: 讓view和model能溝通,使得model運算的結果可以更新到view上,例如更新在text field上。
model: 實際做的function,例如1加到100
view: user看到的GUI
controller: 讓view和model能溝通,使得model運算的結果可以更新到view上,例如更新在text field上。
declared property
目的: 節省自己寫getter, setter來access instance variable的時間。
how to define:
@property(attributes) Type variableNameList;
ex: @prperty(copy) NSString *name;
where is the location to define?
in header file, after close brace ( } )
how to define method:
use synthesize, the compiler will define accessor methods for you:
ex: @synthesize dog;
how to define:
@property(attributes) Type variableNameList;
ex: @prperty(copy) NSString *name;
where is the location to define?
in header file, after close brace ( } )
how to define method:
use synthesize, the compiler will define accessor methods for you:
ex: @synthesize dog;
interface builder and nib
use interface builder to create GUI
nib:(NeXT interface builder): created by interface builder
double-click on nib file will open interface builder. Then you can modify GUI
outlet: an instance variable that points to other object
when a object A want to send messages to another object B, it must defined B as its outlet
outlet is defined as IBOutlet:
ex: IBOutlet NSTextField textfield;
action: 要做的事情
action is defined as IBAction
ex: (IBAction) action: (id) sender
設定outlet and action:
ctrl+ mouse
If A want to send messages to B, connect from A to B
how to create a object in interface builder:
define class' header
In Interface builder, choose File-> read class files , choose class' header file
move object from library to mainMenu.nib
change object's class to your class name( under class identify)
nib:(NeXT interface builder): created by interface builder
double-click on nib file will open interface builder. Then you can modify GUI
outlet: an instance variable that points to other object
when a object A want to send messages to another object B, it must defined B as its outlet
outlet is defined as IBOutlet:
ex: IBOutlet NSTextField textfield;
action: 要做的事情
action is defined as IBAction
ex: (IBAction) action: (id) sender
設定outlet and action:
ctrl+ mouse
If A want to send messages to B, connect from A to B
how to create a object in interface builder:
define class' header
In Interface builder, choose File-> read class files , choose class' header file
move object from library to mainMenu.nib
change object's class to your class name( under class identify)
memory management in objective-c
before objective-c 2.0, there is no garbage collection
Hence, it is necessary to manage memory
every variable has a retain count. when its retain count becomes 0, it is released.
retain count在什麼時候會+1
(1)use "retain" on object: ex [dog retain]
(2) object created by "alloc", "copy", "new", "mutableCopy"
(3) when object is add to array
retain count在什麼時候會-1
(1)use "release" on object
(2)when object is removed from array
autorelease pool:
objects are added to autorelease pool when they are sent message "autorelease".
when the autorelease pool is deallocated, it sends message "release" to objects in the poll
Hence, it is necessary to manage memory
every variable has a retain count. when its retain count becomes 0, it is released.
retain count在什麼時候會+1
(1)use "retain" on object: ex [dog retain]
(2) object created by "alloc", "copy", "new", "mutableCopy"
(3) when object is add to array
retain count在什麼時候會-1
(1)use "release" on object
(2)when object is removed from array
autorelease pool:
objects are added to autorelease pool when they are sent message "autorelease".
when the autorelease pool is deallocated, it sends message "release" to objects in the poll
2008年2月26日 星期二
2008年2月25日 星期一
NSObject
method:
isMemberOfClass:
判斷object是否是某個class
isKindOfClass:
判斷object是否是某個class 或是inherit from that class
isMemberOfClass:
判斷object是否是某個class
isKindOfClass:
判斷object是否是某個class 或是inherit from that class
2008年2月24日 星期日
garbage collection in objective-c
default, gc is not enabled.
how to enable it:
in Xcode, in the target, in the build panel,
under gcc-4.0,
in Objective-c garbage collection, choose supported( -fobjc-gc)
This means your application can uses gc & retain/release method
If choose required( -fobjc-gc-only) , your application can only use gc
required mode is more recommended.
how to enable it:
in Xcode, in the target, in the build panel,
under gcc-4.0,
in Objective-c garbage collection, choose supported( -fobjc-gc)
This means your application can uses gc & retain/release method
If choose required( -fobjc-gc-only) , your application can only use gc
required mode is more recommended.
objective-c syntax
use import to include header file
define class:
@interface Dog: NSObject {
int head;
}
-(void) print: (int) d;
@end;
(1) Dog extends NSObject, @interface = Java's class
(2) instance variables are defined between { }
(3) default access is protected
(4) method's syntax: - (return type) name: (parameter1 type) parameter;
- means instance method, static method(class method) uses +
how to access public instance variable
ex: ceo object has instance variable boss,
ceo -> boss
how to define method with multiple parameters:
ex: -(void) eat: (int) n andDrink: (int) d;
andDrink is label name. It is optional, but is recommended
how to call method:
[object method], ex: [dog eat] = dog->method in Java
objective-c does not have value type. It is always pointer type. Hence, [dog eat] = dog->method, not dog.method
how to call method with parameters
ex: (1)[dog eat: 1 :2]
1 and 2 are the parameters
(2) [dog eat: 1 drink:2]
1 and 2 are the parameters
alloc and init method:
alloc: allocate memory, ex: [Dog alloc]
init: initialize variable in the object
Use alloc and init to create new object: ex: Dog *dog = [[Dog alloc] init];
how to define constructor :
call "self = [super init];" in the constructor definition
self is like "this" in Java, super means parent, call parent's init method to init object
nil:
nil = NULL in C
access privileges:
ex:
@public
int a;
@private
int b;
@protected
int c
id:
a type, a general pointer to an object, the type of object is determined at runtime
self and super:
self = this in Java
super = super in Java
define class:
@interface Dog: NSObject {
int head;
}
-(void) print: (int) d;
@end;
(1) Dog extends NSObject, @interface = Java's class
(2) instance variables are defined between { }
(3) default access is protected
(4) method's syntax: - (return type) name: (parameter1 type) parameter;
- means instance method, static method(class method) uses +
how to access public instance variable
ex: ceo object has instance variable boss,
ceo -> boss
how to define method with multiple parameters:
ex: -(void) eat: (int) n andDrink: (int) d;
andDrink is label name. It is optional, but is recommended
how to call method:
[object method], ex: [dog eat] = dog->method in Java
objective-c does not have value type. It is always pointer type. Hence, [dog eat] = dog->method, not dog.method
how to call method with parameters
ex: (1)[dog eat: 1 :2]
1 and 2 are the parameters
(2) [dog eat: 1 drink:2]
1 and 2 are the parameters
alloc and init method:
alloc: allocate memory, ex: [Dog alloc]
init: initialize variable in the object
Use alloc and init to create new object: ex: Dog *dog = [[Dog alloc] init];
how to define constructor :
call "self = [super init];" in the constructor definition
self is like "this" in Java, super means parent, call parent's init method to init object
nil:
nil = NULL in C
access privileges:
ex:
@public
int a;
@private
int b;
@protected
int c
id:
a type, a general pointer to an object, the type of object is determined at runtime
self and super:
self = this in Java
super = super in Java
2008年2月23日 星期六
2008年2月20日 星期三
2008年2月19日 星期二
static and extern
default( global variable/function前不加東西)
his variable/ function在此定義,且可以別的檔案存取。
在global variable/function前加上static:
his variable/ function只能在這個檔案使用
在global variable/function前加上extern:
this variable/ function在別的檔案定義
his variable/ function在此定義,且可以別的檔案存取。
在global variable/function前加上static:
his variable/ function只能在這個檔案使用
在global variable/function前加上extern:
this variable/ function在別的檔案定義
little endian and big endian
little endian:
the least significant byte comes first
ex:
0x1122 is store as 22 11
the least significant byte comes first
ex:
0x1122 is store as 22 11
2008年2月18日 星期一
compile with framework
compile files that connect to user client in the kernel:
IOKit, CoreFoundation, ApplicationServices
IOKit, CoreFoundation, ApplicationServices
2008年2月17日 星期日
JNI
檔名:
.jnilib : mac os x , prefixed by lib and suffixed with .jnilib
.so : solaris and linux
.dll: windows
in library file:
(1) include
(2) function格式:
ex: JNIEXPORT jint JNICALL Java_Test_show(JNIEnv *env, jobject rkObject)
function name = Java_Test_show, 以Java開頭,接著是溝通的Java的class name,最後才是function name
如果Java是在某個package上,要包含package name, 例如class Dog在package Animal下,
則完整的function name是Java_Animal_Dog_eat()
JNIEXPORT and JNICALL make this function to be found
(3)compile:
gcc -c -I /System/Library/Frameworks/JavaVM.framework/Headers test.c
如果是在xcode,在target info panel下的 build tab下的Header Search Paths設成/System/Library/Frameworks/JavaVM.framework/Headers
(4) product jnilib file
gcc -dynamiclib -o libtest.jnilib test.o -framework JavaVM
in Java
(1) check path, jnilib檔的位置要在path上
System.out.println( System.getProperty( "java.library.path" ) );
(2) loads the library named libhello.jnilib
System.loadLibrary("hello")
(3) 宣告要呼叫的library function
ex: public native void show();
以public native開頭
把Array 的結果copy back to 呼叫端
(*env)->ReleaseIntArrayElements(env, ctrllist, jBody, 0);
.jnilib : mac os x , prefixed by lib and suffixed with .jnilib
.so : solaris and linux
.dll: windows
in library file:
(1) include
(2) function格式:
ex: JNIEXPORT jint JNICALL Java_Test_show(JNIEnv *env, jobject rkObject)
function name = Java_Test_show, 以Java開頭,接著是溝通的Java的class name,最後才是function name
如果Java是在某個package上,要包含package name, 例如class Dog在package Animal下,
則完整的function name是Java_Animal_Dog_eat()
JNIEXPORT and JNICALL make this function to be found
(3)compile:
gcc -c -I /System/Library/Frameworks/JavaVM.framework/Headers test.c
如果是在xcode,在target info panel下的 build tab下的Header Search Paths設成/System/Library/Frameworks/JavaVM.framework/Headers
(4) product jnilib file
gcc -dynamiclib -o libtest.jnilib test.o -framework JavaVM
in Java
(1) check path, jnilib檔的位置要在path上
System.out.println( System.getProperty( "java.library.path" ) );
(2) loads the library named libhello.jnilib
System.loadLibrary("hello")
(3) 宣告要呼叫的library function
ex: public native void show();
以public native開頭
把Array 的結果copy back to 呼叫端
(*env)->ReleaseIntArrayElements(env, ctrllist, jBody, 0);
2008年2月16日 星期六
2008年2月12日 星期二
2008年2月1日 星期五
訂閱:
文章 (Atom)