在 Objective-C 中保留 C 数组

发布于 2024-09-15 16:16:57 字数 159 浏览 4 评论 0原文

我在firstClass中创建c数组(这是在界面中创建的):

BOOL        taken[25];

然后我转到不同的视图并返回到第一个视图,除了我的c数组重置为0,

当我返回时如何保留我的数组观点之间的来回转换?

i create a c 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-09-22 16:16:57

您无法将 retain 消息发送到纯 C 数组。应用普通的 C 内存管理。即,当超出范围时,局部堆栈变量将消失,全局变量将存在,等等。如果您需要“长期存在”的内存,请使用动态分配的内存(C++ 中的 malloc 或 new),但您有责任释放 /em> 当你完成它时。

You cannot send retain messages to plain C arrays. Normal C memory management applies. I.e. 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.

素衣风尘叹 2024-09-22 16:16:57

像“BOOL take[25]”这样的直接数组的生命周期与其所在对象的生命周期相同。如果周围的对象被释放,则数组也会随之消失;相反,如果保留周围的对象,则数组也保留。因此,要保留此数组,请确保视图未释放,并确保它与上次使用的视图对象相同。

特别是就 iOS 视图控制而言(我认为您要问的是),请尝试在“视图控制器”中编写业务逻辑,并让它重用 UIView 对象。或者,如果您不重复使用相同的视图,至少让它将新视图(包括数组)初始化为正确的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文