简体   繁体   English

保留Objective-c中的C数组

[英]retain C array in objective-c

i create ac array in the firstClass (this is created in the interface): 我在firstClass中创建了一个ac数组(这是在接口中创建的):

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, 然后我转到另一个视图并回到第一个视图,除了我的c数组重置为0,

how do i retain my array when i go back an forth between views? 当我在视图之间返回时,如何保留我的数组?

You cannot send retain messages to plain C arrays. 您不能将保留消息发送到普通C阵列。 Normal C memory management applies. 正常的C内存管理适用。 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. 即,当超出范围,全局变量将存在时,本地堆栈变量将逐渐消失,等等。如果您需要“长寿”内存,请使用动态分配的内存(malloc或C ++中的新内存),但是当您需要时,您有责任将其释放 。完成它。

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; 像“BOOL take [25]”这样的直接数组的生命周期与它所在对象的生命周期相同。如果周围的对象被释放,则数组随之而来; 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. 特别是在iOS视图控制方面(我认为你在问什么),尝试在“视图控制器”中编写业务逻辑,并重新使用UIView对象。 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. 或者,如果您不重复使用相同的视图,至少要将新视图(包括数组)初始化为正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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