Differences between [NSArray arrayWithArray:] and [NSArray copy]

前端 未结 7 2158
清歌不尽
清歌不尽 2021-02-12 13:50

lately I work much with arrays and I\'m wonder.. what\'s diffrences between those two lines.

NSArray *array = [NSArray arrayWithArray:someArray];
7条回答
  •  天命终不由人
    2021-02-12 14:27

    The difference between the two is that the latter will be retained. The former will be autoreleased.

    Both versions make a shallow copy of the array.

    NSMutableArray *notMutableReally = [NSArray arrayWithArray:aMutableArray];
    

    Should give you a compiler warning as you will be trying to assign a NSArray to a NSMutableArray.

    Use.

    NSMutableArray *mutableArrayCopy = [NSMutableArray arrayWithArray:aMutableArray];
    

    Which is faster? Dont worry, they are all far faster than the rest of the stuff you will be doing. Check with Instruments if you really care.

提交回复
热议问题