isKindOfClass和isMemberOfClass

- isKindOfClass:

返回一個(gè)布爾類型浊竟,來判斷某個(gè)對(duì)象是不是給出的類,或繼承自它的類的實(shí)例對(duì)象津畸。

用來判斷類簇時(shí)要小心振定,可能得到的不是你想要的值

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

Discussion

For example, in this code, isKindOfClass: would return YES because, in Foundation, the NSArchiver class inherits from NSCoder:

NSMutableData *myData = [NSMutableData dataWithCapacity:30];

id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];

if ( [anArchiver isKindOfClass:[NSCoder class]] )

...

Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:

// DO NOT DO THIS!

if ([myArray isKindOfClass:[NSMutableArray class]])

{

// Modify the object

}

If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

If the receiver is a class object, this method returns YES if aClass is a Class object of the same type, NO otherwise.

- isMemberOfClass:

僅用以判斷某個(gè)對(duì)象是不是給出類的實(shí)例對(duì)象。

Returns a Boolean value that indicates whether the receiver is an instance of a given class.

Discussion

For example, in this code, isMemberOfClass: would return NO:

NSMutableData *myData = [NSMutableData dataWithCapacity:30];

id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];

if ([anArchiver isMemberOfClass:[NSCoder class]])

...

Class objects may be compiler-created objects but they still support the concept of membership. Thus, you can use this method to verify that the receiver is a specific Class object.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末肉拓,一起剝皮案震驚了整個(gè)濱河市后频,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌暖途,老刑警劉巖卑惜,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異驻售,居然都是意外死亡露久,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門欺栗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來毫痕,“玉大人,你說我怎么就攤上這事迟几∠耄” “怎么了?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵类腮,是天一觀的道長臊泰。 經(jīng)常有香客問我,道長蚜枢,這世上最難降的妖魔是什么缸逃? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任七婴,我火速辦了婚禮,結(jié)果婚禮上察滑,老公的妹妹穿的比我還像新娘打厘。我一直安慰自己,他們只是感情好贺辰,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布户盯。 她就那樣靜靜地躺著,像睡著了一般饲化。 火紅的嫁衣襯著肌膚如雪莽鸭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天吃靠,我揣著相機(jī)與錄音硫眨,去河邊找鬼。 笑死巢块,一個(gè)胖子當(dāng)著我的面吹牛礁阁,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播族奢,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼姥闭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了越走?” 一聲冷哼從身側(cè)響起棚品,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎廊敌,沒想到半個(gè)月后铜跑,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡骡澈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年锅纺,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片秧廉。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡伞广,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出疼电,到底是詐尸還是另有隱情嚼锄,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布蔽豺,位于F島的核電站区丑,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜沧侥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一可霎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宴杀,春花似錦癣朗、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扁达,卻和暖如春正卧,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背跪解。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來泰國打工炉旷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叉讥。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓窘行,卻偏偏與公主長得像,于是被迫代替她去往敵國和親节吮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子抽高,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,490評(píng)論 5 6
  • 小表妹要去上大學(xué)了,印象里的小孩也有上大學(xué)的一天透绩,曾經(jīng)物理化學(xué)考30分的人,竟然也長大了壁熄,結(jié)果還是沒上一本線帚豪,只好...
    名貝貝閱讀 606評(píng)論 3 5
  • 最初看到電影名字《地心營救》,下意識(shí)認(rèn)為又是和《2012》草丧、《后天》等一樣的驚險(xiǎn)災(zāi)難場面電影狸臣,準(zhǔn)備要來一場虛擬驚險(xiǎn)...
    stone_thinker閱讀 2,057評(píng)論 2 1
  • 別笑著面對(duì)一切 低下頭來 看著無力的雙腳 還有那 徘徊不前的足跡 一陣沉默 一陣思考 留下的只是難言的嘆息 你說過...
    月橋居士閱讀 330評(píng)論 0 0