iOS面试小结

1. @property (copy) NSMutableArray *array; 这样写有什么问题吗?

因为用了copy, 内部会深拷贝一次, 指针实际指向的是NSArray, 所以如果调用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).

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,811评论 0 23
  • 【男票版】 他会把你当作小孩子宠。 会拍掉你在逛超市时一直伸向零食的手。 会在你环抱肩膀在路边小店避雨时赶过来关心...
    礼雪晶阅读 282评论 0 16
  • 书是作者内心的投影,不同的读者有不同的见解。 风景是大自然的杰作,不同的人看到的也不尽相同。只有自己看到了,体会到...
    雨微尘阅读 386评论 0 2