简体   繁体   中英

retain C array in objective-c

i create ac array in the firstClass (this is created in the interface):

BOOL        taken[25];

i then go to a different view and come back to the first one, except my c array is reset to 0,

how do i retain my array when i go back an forth between views?

You cannot send retain messages to plain C arrays. Normal C memory management applies. Ie local stack variables will fade away when out of scope, global variables will live, etc. Use dynamically allocated memory (malloc or new in C++) if you need "long-living" memory, but you are responsible to free it when you're done with it.

The lifetime of an immediate array like "BOOL taken[25]" is the same as the lifetime of the object that it is in. If the surrounding object gets deallocated the array goes with it; conversely, if the surrounding object is retained then so is the array. So to keep this array around, make sure the view is not deallocated, and make sure it's the same view object as you used last time.

In terms of iOS view control in particular (which is I think what you're asking about), try to write your business logic in your "view controller", and have it re-use UIView objects. Or, if you don't re-use the same view, at least have it initialize the new view (including the array) to the correct value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM