天天看點

object-c 基礎小結

一  基本文法

1 頭檔案.h,實作檔案.m

2  import 是引用

3  頭檔案中用

@interface開始一個類的定義,

@end

結束

.m檔案中用

@implementation開始實作

@end

結束

4 函數寫法

-(void)setNumerator: (int) n;

-(void)setNumerator: (int) n andDenominator: (int) d;

method:label1:label2:label3:,而呼叫的方法是 [obj method:param1 label1: param2 label2: param3 label3: param4]

這個是頭檔案中的申明,-就表示成員函數,如果是加号,那麼就相當于static,

setNumerator是函數名稱,void是傳回,後面的(int) n,是參數,如果沒有參數,那麼就直接-(void) setNumerator;

5函數調用

在Objective-C 中呼叫 methods 的方法是[object method],就像 C++ 的object->method()。

Eg:

[fracsetNumerator: 1]; //frac是對象指針,1是參數

[frac2setNumerator: 1 andDenominator: 5];

Objective-C沒有 value 型别。是以沒有像 C++ 的 Fraction frac; frac.print(); 這類的東西。在 Objective-C 中完全使用指針來處理對象。

6  oc中用self 代貼this

self = [super init];

nil是 Objective-C 用來表達 C/C++ 中 NULL 的方式,

if(self != nil )

7 通路權限

預設的權限是 @protected

@interface Access: NSObject {

§ @public

§ int publicVar;

§ @private

§ int privateVar;

§ int privateVar2;

§ @protected

§ int protectedVar;

§ }

@end

如同你所看到的,就像 C++ 中 private: [list of vars] public: [listof vars] 的格式,它隻是改成了@private, @protected, 等等。

8 異常情況(Exceptions)

注意:異常處理隻有 Mac OS X 10.3 以上才支援。

#import<Foundation/NSException.h>

@try {

§ [cup fill];

§ } @catch (CupWarningException *e ) {

§ printf( "%s: ",[[e name] cString] );

§ } @catch (CupOverflowException *e ) {

§ printf( "%s: ",[[e name] cString] );

§ } @finally {

§ [cup print];

§ }

9 動态識别(Dynamic types)

-(BOOL) isKindOfClass: classObj is object a descendent or member of classObj 此對象是否是 classObj 的子孫或一員
-(BOOL) isMemberOfClass: classObj is object a member of classObj 此對象是否是 classObj 的一員
-(BOOL) respondsToSelector: selector does the object have a method named specifiec by the selector
                        此對象是否有叫做    selector 的 method                                                                
+(BOOL) instancesRespondToSelector: selector does an object created by this class have the ability to respond to the specified selector 此對象是否是由有能力響應指定 selector 的對象所産生
-(id) performSelector: selector invoke the specified selector on the object 喚起此對象的指定 selector

10  Category 類目

Category提供一種為某個類添加方法而又不必編寫子類的途徑。

比如  有一個類MyClass,現在我又想再加個add方法在裡面,

@interface MyClass(Math)

§ -(MyClass*) add: (MyClass*) f;

@end

 就沒有用繼承了。

如果你新加的函數和類之前的函數同名,那麼在[object method]的時候,優先調用類本身以前的方法

注:如果interface的方法,和

implementation的方法都在.m檔案中  那麼這個方法就相當于是private了,因為interface不在.h中,那麼别的檔案也就import不到了,就相當于是private了

11 Posing 扮演

Posing有點像 categories,但是不太一樣。它允許你擴充一個 class,并且全面性地的扮演(pose)這個 super class。例如:你有一個擴充 NSArray 的 NSArrayChild 物件。如果你讓 NSArrayChild 扮演 NSArray,則在你的程式代碼中所有的 NSArray 都會自動被替代為 NSArrayChild。

12 Protocols 協定

Objective-C裡的 Protocol 與 Java 的 interface 或是C++ 的 purely virtual class 相同。

@protocol Printing

§ -(void) print;

@end

@interface Fraction: NSObject <Printing, NSCopying> {

§ int numerator;

§ int denominator;

§ }

就像使用@selector來測試對象的繼承關系,你可以使用 @protocol 來測試對象是否遵從接口。如果對象遵從這個接口,[object conformsToProtocol: @protocol( SomeProtocol )] 會回傳一個 YES 型态的 BOOL 對象。同樣地,對 class 而言也能如法炮制 [SomeClass conformsToProtocol:@protocol( SomeProtocol )]。

13 記憶體管理

Retain and Release

引用計數

[frac1 retainCount]   檢視

[frac1 retainCount]  加1

[frac1 release];  減1

當retainCount減到0時,就自動調用dealloc ,銷毀自己

Autorelease Pool

千萬記得,要有良好的記憶體管理,像 [NSStringstringWithString: @"String"] 這種 method 使用了 autorelease pool,而 alloc method 如 [[NSString alloc]initWithString: @"String"] 則沒有使用 auto release pool。

Autorelease好處,就是調用一次,就可以不管它了(調用的時候,當然沒有被release掉),知道最後[pool release],那個時候,就會把這些該release的release掉了

14 Foundation framework classes   架構(c++的模闆)

Foundationframework 地位如同 C++ 的 Standard Template Library。不過 Objective-C 是真正的動态識别語言(dynamic types),是以不需要像 C++ 那樣肥得可怕的樣版(templates)。這個 framework 包含了對象組、網絡、線程,還有更多好東西。

NSArray

NSArray跟 NSMutableArray,顧名思義,mutable(善變的)表示可以被改變,而 NSArray 則不行。這表示你可以制造一個 NSArray 但卻不能改變它的長度。

NSArray *arr = [[NSArray alloc] initWithObjects:

§ @"Me",@"Myself", @"I", nil];    nil是結尾符号

[[objdescription] cString]

descriptionmethod。它就像 Java 的 toString,會回傳對象的 NSString 表示法。

NSDictionary   有點像map

15 優點與缺點

優點

§ Cateogies

§ Posing

§ 動态識别

§ 名額計算

§ 彈性訊息傳遞

§ 不是一個過度複雜的 C 衍生語言

§ 可透過 Objective-C++ 與 C++ 結合

o 缺點

§ 不支援命名空間

§ 不支援運算符多載(雖然這常常被視為一個優點,不過正确地使用運算符多載可以降低程式代碼複雜度)

§ 語言裡仍然有些讨厭的東西,不過不比 C++ 多