Does NSArray initWithObjects: retain objects?

前端 未结 2 1901
生来不讨喜
生来不讨喜 2021-01-21 04:42

When adding objects to an NSArray using \"initWithObjects\" can anyone confirm for me that the items are retained. I am pretty sure they are, but can\'t find it mentioned anywhe

相关标签:
2条回答
  • 2021-01-21 05:16

    initWithObjects retains all items in the array.

    initWithObjects: count:

    • (id) initWithObjects: (id*)objects count: (NSUInteger)count; Availability: OpenStep

    This is a designated initialiser for the class. Subclasses must override this method. This should initialize the array with count (may be zero) objects. Retains each object placed in the array. Calls -init (which does nothing but maintain MacOS-X compatibility), and needs to be re-implemented in subclasses in order to have all other initialisers work.

    0 讨论(0)
  • 2021-01-21 05:24

    Objects are expected to take ownership of the things they need to keep around. An array is responsible for its items, therefore it retains them. See the memory management guide for complete details. (No, seriously, read it. You'll thank yourself later when you don't have to ask this question about every class you use and your program isn't crashing every five seconds.)

    0 讨论(0)
提交回复
热议问题