What happens when an NSArray is freed?

Suppose I create several objects and add them to an array.

House *myCrib = [House house];
House *johnHome = [House house];
House *lisaHome = [House house];
House *whiteHouse = [House house];

NSArray *houses = [NSArray arrayWithObjects: myCrib, johnHome, lisaHome, whiteHouse, nil];

As a rule, all objects of the House have a retention rate of two times, but once they are implemented automatically. After some time, I decided to free myCrib, even if I am not the owner - I was never saved and was not initialized.

[myCrib release];

The save count should drop to zero, and my object should be freed. Now my question is: will this illegal action cause my application to work erroneously or even crash or will it NSArrayjust delete my object from its list with bad consequences.

I am looking for a way to keep a list of objects, but I want the list to support itself. When some object disappears, I want the link to it to disappear from my array gracefully and automatically. I am thinking of a subclass or wrapper NSArray.

Thank.

+3
source share
5 answers

myCrib, . . , - . ; , - .

, : , . , , . , .


, ( ):

  • NSPointerArray - . , (.. , ). , iOS.
  • CFMutableArrayRef - retain release . , . .
  • DDAutozeroingArray - NSMutableArray, , , Mac OS, iOS. ; , , . https://github.com/davedelong/Demos
+2

: , NSArray .

. , , , , . , , , , . , , , - , , NSArray .

, . - , .

, , / / , . , -retain - , , , , , ; , .

Core Data. , . , to-many - , , .

: , ios. MacOS X, NSPointerArray. , NSPointerArray . , , , .

+6

, . - , . NSArray.

, , , . .

+3

. NSArray - , , , ?

NSArray . , , , , , .

, . - , , , . , .

+1

I created a wrapper class - in my code it was called a controller that supports an (mutable) array for me. I initialize the controller class in my view controllers — the place I need them, instead of using the array directly.

Invalid code for me. :-P

0
source

All Articles