NSArray/NSMutableArray :通过引用传递还是通过值传递?

发布于 2024-08-29 02:08:18 字数 1925 浏览 5 评论 0原文

这里完全混乱了。

我有一个父 UIViewController 需要将 NSMutableArray 传递给子 UIViewController。我希望它通过引用传递,以便在 CHILD 中所做的更改将反映在 PARENT 中,反之亦然。但事实并非如此。两者都有一个声明为 ..

@property (nonatomic, keep) NSMutableArray *photos 的属性;

示例:

在父母中:

self.photos = [[NSMutableArray alloc] init];

ChildViewController *c = [[ChildViewController alloc] init ...];
c.photos = self.photos;
...
...

...

在孩子中:

[self.photos addObject:obj1];
[self.photos addObject:obj2];

NSLog(@"Count:%d", [self.photos count]) // Equals 2 as expected

...

回到父母中:

NSLog(@"Count:%d", [self.photos count])  // Equals 0 ... NOT EXPECTED

我以为他们都会访问相同的内存。难道不是这样吗?如果不是......我如何保持两个 NSMutableArrays 同步?

用更多代码更新...

在父 UIViewController 中:

     - (void)loadView { 

         self.photos = [[NSMutableArray alloc] init];

         // Create view for root controller
     UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     self.view = rootView;
     self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"green_felt_bg.jpg"]];
     [rootView release];

     ChildViewController *c = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
     self.childViewController = c;
     [c release];

     self.childViewController.photos = self.photos;
     self.childViewController.delegate = self;

     [self.view insertSubview:self.childViewController.view atIndex:0];
 } 

在子 ViewController 中,我只是向其中添加对象。添加完 PARENT 处理的对象后调用委托方法。没有其他重要的事情发生。代码 child 正在使用修改其“照片”属性..

[[self mutableArrayValueForKey:@"photos"] 
        addObject:[photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(200.0f, 300.0f) interpolationQuality:kCGInterpolationLow]];

Totally confused here.

I have a PARENT UIViewController that needs to pass an NSMutableArray to a CHILD UIViewController. I'm expecting it to be passed by reference so that changes made in the CHILD will be reflected in the PARENT and vice-versa. But that is not the case. Both have a property declared as ..

@property (nonatomic, retain) NSMutableArray *photos;

Example:

In PARENT:

self.photos = [[NSMutableArray alloc] init];

ChildViewController *c = [[ChildViewController alloc] init ...];
c.photos = self.photos;
...
...

...

In CHILD:

[self.photos addObject:obj1];
[self.photos addObject:obj2];

NSLog(@"Count:%d", [self.photos count]) // Equals 2 as expected

...

Back in PARENT:

NSLog(@"Count:%d", [self.photos count])  // Equals 0 ... NOT EXPECTED

I thought they'd both be accessing the same memory. Is this not the case? If it isn't ... how do I keep the two NSMutableArrays in sync?

UPDATE with some more code ...

IN PARENT UIViewController:

     - (void)loadView { 

         self.photos = [[NSMutableArray alloc] init];

         // Create view for root controller
     UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
     self.view = rootView;
     self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"green_felt_bg.jpg"]];
     [rootView release];

     ChildViewController *c = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
     self.childViewController = c;
     [c release];

     self.childViewController.photos = self.photos;
     self.childViewController.delegate = self;

     [self.view insertSubview:self.childViewController.view atIndex:0];
 } 

In the CHILD ViewController I'm just adding objects into it. Calling a delegate method when it is done adding objects ... which the PARENT handles. Nothing else significant going on. Code child is using to modify its "photos" property ..

[[self mutableArrayValueForKey:@"photos"] 
        addObject:[photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(200.0f, 300.0f) interpolationQuality:kCGInterpolationLow]];

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

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

发布评论

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

评论(2

遥远的绿洲 2024-09-05 02:08:18

你是对的,这些属性值是指针,所以这里应该只有一个可变数组。我同意 andyvn22 的观点,即发生了其他事情,在您迄今为止发布的代码中并不明显。

这是一种可能的调试方法:在每个 NSLog 上设置一个断点,当您进入调试器时,使用调试窗口 (cmd-shift-Y) 检查每种情况下 photos 属性的实际内存地址。只有两种可能性:

  1. 地址相同,在这种情况下,它确实是同一个数组,并且您没有想到的一些代码正在改变第一个日志和第二个日志之间的数组;或者,

  2. 地址不同,即它们是不同的数组,并且您没有想到的一些代码正在将新数组重新分配给子(或父)照片属性。

祝你好运!

You're right, those property values are pointers, so there should be only one mutable array here. I agree with andyvn22 that something else is going on, something not evident in the code you've posted so far.

Here's a possible way to debug it: set a breakpoint on each of those NSLogs, and when you drop into the debugger, use the debug window (cmd-shift-Y) to examine the actual memory address of the photos property in each case. There are only two possibilities:

  1. The addresses are the same, in which case it really is the same array, and some code you're not thinking of is mutating the array between the first log and the second; or,

  2. The addresses are different, i.e. they're different arrays, and some code you're not thinking of is reassigning a new array to the child (or parent) photos property.

Good luck!

萌化 2024-09-05 02:08:18

最简单的方法是不使用合成的访问器方法,而只使用您自己的 get/set 方法,专门将指针引用设置为相等。

查看 关于属性的 Apple 文档,即使您不使用 @synthesize 指令,您仍然可以对属性使用方便的点表示法。

我的猜测是您希望它们看起来像这样:

-(NSMutableArray*)photos
{
    return photos_internal_pointer;
}
-(void)setPhotos:(NSMutableArray*)newValue
{
    [newValue retain];
    [photos_internal_pointer release];
    photos_internal_pointer = newValue;
}

当然,其中 photos_internal_pointer 的类型是 NSMutableArray*。这应该使它们保持同步,因为将指针设置为彼此相等将使它们指向同一个对象。

The easiest way to do this would be to not use the synthesized accessor methods, and just roll your own get/set methods that specifically set the pointer references equal.

Looking at the apple documentation on properties, you can still use the convenient dot notation with properties even if you don't use the @synthesize directive.

My guess is you'll want them to look something like:

-(NSMutableArray*)photos
{
    return photos_internal_pointer;
}
-(void)setPhotos:(NSMutableArray*)newValue
{
    [newValue retain];
    [photos_internal_pointer release];
    photos_internal_pointer = newValue;
}

Where photos_internal_pointer is of type NSMutableArray*, of course. This should keep them in sync because setting the pointers equal to each other will have them point to the same object.

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