iOS面試小結(jié)

1. @property (copy) NSMutableArray *array; 這樣寫有什么問題嗎?

因為用了copy, 內(nèi)部會深拷貝一次, 指針實際指向的是NSArray, 所以如果調(diào)用removeObject和addObject方法的話, 會unRecognized selector?

-copy, as implemented by mutable Cocoa classes, alwaysreturns their immutable counterparts. Thus, when an NSMutableArray is sent -copy, it returns an NSArray containing the same objects.

Becausewordshas the memory qualifiercopy, this line:

NSMutableArray *mutWords = [[NSMutableArray alloc] initWithArray:fixedWords];

self.words = mutWords;

Expands out to:

NSMutableArray *mutWords = [[NSMutableArray alloc] initWithArray:fixedWords];

self.words = [mutWords copy];

Given that NSMutableArray is a subclass of NSArray, the compiler doesn't complain, and you now have a ticking time bomb on your hands because NSArray does not recognize it's mutable subclass' methods (because it cannot mutate it's contents).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,036評論 0 23
  • 【男票版】 他會把你當(dāng)作小孩子寵。 會拍掉你在逛超市時一直伸向零食的手。 會在你環(huán)抱肩膀在路邊小店避雨時趕過來關(guān)心...
    禮雪晶閱讀 301評論 0 16
  • 書是作者內(nèi)心的投影,不同的讀者有不同的見解。 風(fēng)景是大自然的杰作,不同的人看到的也不盡相同。只有自己看到了,體會到...
    雨微塵閱讀 464評論 0 2

友情鏈接更多精彩內(nèi)容