OC中的 和isEqual方法

2021-06-28 03:40:56 字數 1522 閱讀 8886

//  main.m

//  oc中的==和isequal方法

//  created by goddog on 15/1/10.

1.如果兩個變數時基本資料型別的變數,都是數值型,使用==判斷酒返回真。

2.對應兩個指標型別的變數,它們必須指向同一物件(兩個指標變數儲存的記憶體位址相同)時,==判斷才會返回真。

3.常量池中保證相同的字串直接量只有乙個,不會產生多個副本。

4.使用nsstring stringwithformat:類方法建立的字串物件時執行時建立出來的,它被儲存在執行時記憶體區內(堆記憶體),不會放入常量池中。

5.對於兩個字串變數,可能只是要求他們指向字串物件裡包含的字串行相同即可認為相等,此時就可用isequal:方法。

6.我們大多時候可以重寫isequal方法,提供自定義的標準。

7.isequaltostring:方法專門用來判斷當前字串與另乙個字串的字串行是否相等。

//

// prettygirl.h

// oc中的==和isequal方法

//// created by goddog on 15/1/10.

//#import @inte***ce prettygirl : nsobject

@property (nonatomic,copy) nsstring* name;

@property (nonatomic,copy) nsstring* nation;

-(id) initwithname:(nsstring*) name andnation:(nsstring*) nation;

@end

//

// prettygirl.m

// oc中的==和isequal方法

//// created by goddog on 15/1/10.

//#import "prettygirl.h"

@implementation prettygirl

@synthesize name = _name;

@synthesize nation = _nation;

//實現

-(id) initwithname:(nsstring *)name andnation:(nsstring *)nation

return self;

}//重寫isequal:方法,提供自定義的標準

-(bool) isequal:(id)other

//如果不為空,並且是類的例項時

if (other != nil && [other ismemberofclass:prettygirl.class])

return no;

}@end

#import #import "prettygirl.h"

int main(int argc, const char * argv)

return 0;

}

OC中的方法

m 是乙個實現檔案 用來實現具體的功能和方法 讓實現檔案 知道自己是什麼 基於什麼 需要匯入宣告檔案 就是把整個 h檔案讓 m共享 import person.h implementation 表示實現 表示人類具體功能方法實現功能的開始 implementation person oc裡面方法的公...

ObjC中isEqual與 的區別

isequal是nsobject的方法,我們常用它來判斷兩個object是否相等,而 也常用來來判斷兩個object是否相等,那麼它們有什麼區別呢。看一段 就明白了 inte ce myitem nsobject property nonatomic,copy nsstring identifier...

OC 關於OC中的物件方法和類方法的相互呼叫1

物件方法和類方法的區別和概念,大家都知道,關於兩者之間的相互呼叫,本人小有總結 import nsstring ww.h implementation nsstring ww 類方法 int numbercountofstring nsstring str int count 0 for int i...