NSMutable 数组 - 分配和保留对象

发布于 2024-10-18 05:37:21 字数 229 浏览 2 评论 0原文

我需要一些有关如何分配、保留对象的信息。

例如 - 如果我们有两个视图控制器并且需要将数组数据从 viewcontrlr 1 传递到 viewContrl 2,我们如何将对象从视图 1 发送到视图 2 并在视图 1 中释放它并在视图 2 中保留它。

一个简单的 =运算符只是分配再次指向视图 1 对象的地址。当从视图 1 传递时,我们可以释放视图 1 中的 obj 并在视图 2 中保留新对象,最好的方法是什么。

I need some info about how to assign, retain objects.

For example - if we have two viewcontrollers and needed to pass an array data from viewcontrlr 1 to viewContrl 2, how can we send the object from view 1 to view 2 and release it in view 1 and retain it in view 2.

A simple = operator is just assigning the address which again points to view 1 object. What is the best way so we can release obj in view 1 and retain a new object in view 2 when passed from view 1.

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

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

发布评论

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

评论(2

七度光 2024-10-25 05:37:21

在视图控制器 2 中创建一个 NSMutableArray 并为其声明一个保留属性。

@interface VC2 : UIViewController
{
   NSMutableArray *mutableArrayInVC2
} 
@property (nonatomic, retain) NSMutableArray *mutableArrayInVC2

然后在您的视图控制器中,您可以通过以下方式传递它:

viewController2Instance.mutableArrayInVC2 = mutableArrayInVC1

并且可以安全地释放它:

[mutableArrayInVC1 release];

[编辑以解决您的评论]

当您为 mutableArrayInVC2 声明保留属性并将 mutableArrayInVC1 传递给它时,“在幕后”,您正在通过其 setter 方法访问变量,如下所示:

-(void)setMutableArrayInVC2:(NSMutableArray *)arrayValue
{
    [arrayValue retain]; // This is your mutableArrayInVC1
    [mutableArrayInVC2 release]; // This is nil the first time you access it which is cool - we can send messages to nil in ObjC
    mutableArrayInVC2 = arrayValue; // So basically you end up doing and assignment but only after retaining the object so it is pointing to the same memory address BUT it is now 'owned' by your VC2 instance.
}

希望它有意义!
罗格

Create a NSMutableArray in your view controller 2 and declare a retain property for it.

@interface VC2 : UIViewController
{
   NSMutableArray *mutableArrayInVC2
} 
@property (nonatomic, retain) NSMutableArray *mutableArrayInVC2

Then in your view controller one you can pass it with:

viewController2Instance.mutableArrayInVC2 = mutableArrayInVC1

And it's safe to release it with:

[mutableArrayInVC1 release];

[EDIT TO ADDRESS YOUR COMMENT]

When you declare a retain property for your mutableArrayInVC2 and pass mutableArrayInVC1 to it, "behind the scenes" you are accessing the variable via its setter method as per below:

-(void)setMutableArrayInVC2:(NSMutableArray *)arrayValue
{
    [arrayValue retain]; // This is your mutableArrayInVC1
    [mutableArrayInVC2 release]; // This is nil the first time you access it which is cool - we can send messages to nil in ObjC
    mutableArrayInVC2 = arrayValue; // So basically you end up doing and assignment but only after retaining the object so it is pointing to the same memory address BUT it is now 'owned' by your VC2 instance.
}

Hope it makes sense!
Rog

倾城月光淡如水﹏ 2024-10-25 05:37:21

另外,您可能还想查看 本文了解有关保留计数机制的一般信息。它与 Mac OS X 的 Cocoa 编程 中的内容非常相似,IMO 就是其中之一关于 Cocoa 和 Obj-C 的最佳入门书籍。我不确定您对 Obj-C/Cocoa 有多少经验,但如果您正在寻找此类介绍,那么这是一个很好的起点。

Also, you may want to check out this article for info on retain count mechanisms in general. It's very similar to what's in Cocoa Programming for Mac OS X which IMO is one of the best intro books on Cocoa and Obj-C in general. I'm not sure how much experience you have with Obj-C/Cocoa but if you're looking for that kind of intro it's a great place to start.

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