美文网首页
iOS面试小结

iOS面试小结

作者: 零纪年 | 来源:发表于2015-07-06 10:40 被阅读184次

    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).

    相关文章

      网友评论

          本文标题:iOS面试小结

          本文链接:https://www.haomeiwen.com/subject/vrpyqttx.html