1. #import
是當初 C: #include 的改良版, 保證標頭檔只會被匯入一次
換言之, 標頭檔 就不用寫成像 :
#ifndef _XXX_H_
#define _XXX_H_
...
#endif
而可以直接宣告
2. @autoreleasepool
3. 物件
C++:
obj->method(argument);
Objective C:
[obj method: argument];
[ ClassOrInstance method ];
myDog = [Dog new];
[myDog run];
4. Interface
@interface NewClassName : ParentClassName
propertyAndMethodDeclarations;
@end
@interface MyObject : NSObject
+(return_type) class_method; // 類方法 -(return_type) instance_method1; // 實體方法 -(return_type) instance_method2: (int) p1; -(return_type) instance_method3: (int) p1 andPar: (int) p2;
@end
+: 類似於 C++ : static function
5. Implementation
@implementation NewClassName
{
memberDeclarations;
}
methodDefinitions;
@end
@implementation MyObject{ int memberVar1; // 實體變數 id memberVar2; }+(return_type) class_method { .... //method implementation } -(return_type) instance_method1 { .... } -(return_type) instance_method2: (int) p1 { .... } -(return_type) instance_method3: (int) p1 andPar: (int) p2 { .... } @end
6. NSObject
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
7. program
@interface Dog: NSObject
-(void) print;
-(void) setName: (NSString*) name;
@end
@implementation Dog
{
NSString *name;
}
-(void) print {
NSLog(@"%@", name);
}
-(void) setName: (NSString*) n {
name = n;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
Dog *myDog;
NSString *name = @"WonWon";
myDog = [Dog new];
[myDog setName: name];
NSLog(@"My dog's name is");
[myDog print];
}
return 0;
}
沒有留言:
張貼留言