0

我有两个由几个对象组成的简单 NSMutableArray。其中一些对象可能是常见的,但需要存储在两个数组中,因为两个数组的用途是为完全不同的目的而定义的。

但是,问题在于,在将相同的对象添加到两个数组之后,在更改一个公共对象的值时,不会反映在第二个数组中。

例如,

假设我们有两个可变的 NSArray:

NSMutableArray *mutableArrayOne;
NSMutableArray *mutableArrayTwo;

现在让我们创建这些数组需要包含的对象定义。

@interface: DummyObject : NSObject
{
     int objectValue;
}

@property (nonatomic) int objectValue;

-(void) printObjectValue;

@end

现在让我们创建基类来存储数组。

基类定义

@interface: BaseClass : NSObject
{
    NSMutableArray *mutableArrayOne;
    NSMutableArray *mutableArrayTwo;
}
-(void) init;
-(void) printBothArrays;

@end

基类实现

@implementation BaseClass

-(void) init
{
     // initialize the mutable array.
     mutableArrayOne = [[NSMutableArray alloc] initWithCapicity:5];
     mutableArrayTwo = [[NSMutableArray alloc] initWithCapicity:5];

     DummyObject *dummyObject = [DummyObject alloc];
     [dummyObject setObjectValue:5];

     DummyObject *dummyObjectTwo = [DummyObject alloc];
     [dummyObjectTwo setObjectValue:2];

     [mutableArrayOne addObject:dummyObject];
     [mutableArrayOne addObject:dummyObjectTwo];

     [mutableArrayTwo addObject:dummyObjectTwo];

}

@end

现在让我修改数组 One 中的 DummyObject:

for (DummyObject* dummyObject in mutableArrayOne)
{
    [dummyObject setValue:100]; 
}

问题 现在,当我打印两个数组对象的值时,问题就开始了:-

打印第一个数组

for (DummyObject* dummyObject in mutableArrayOne)
{
    [dummyObject printObjectValue]; 
}

*输出日志(来自第一个数组)*

100
100

打印第二个数组

for (DummyObject* dummyObject in mutableArrayTwo)
{
    [dummyObject printObjectValue]; 
}

*输出日志(来自第二个数组)*

2

所以在这里我们可以看到 MutableArray 保留了对象的副本,但是,我只想存储引用。这意味着,在更改第一个数组中对象的值时,应该反映在第二个数组中。

我们怎么能做到这一点?我们还有其他选择吗?

谢谢, 帕拉斯·门迪拉塔

4

1 回答 1

0

理想情况下,这将起作用,似乎与设置对象有些问题。例如,您可以像这样检查 -

NSMutableString *firstString = [[NSMutableString alloc] initWithString:@"first"];
NSMutableString *secondString = [[NSMutableString alloc] initWithString:@"second"];

NSMutableArray *originalArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];
NSMutableArray *copyArray = [[NSMutableArray alloc] initWithObjects: firstString, secondString, nil];

[[copyArray objectAtIndex:0] appendString:@"add some text"];

for (int index = 0; index < [originalArray count]; index++) {
    NSLog(@"Original:%@ --- copy:%@", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
}

输出是 -

2012-05-09 10:28:40.382 Demo[5237:f803] Original:firstadd some text --- copy:firstadd some text
2012-05-09 10:28:40.384 Demo[5237:f803] Original:second --- copy:second

编辑 - (在初始化时不添加对象)

NSMutableString *firstString = [[NSMutableString alloc] initWithString:@"first"];
NSMutableString *secondString = [[NSMutableString alloc] initWithString:@"second"];

NSMutableArray *originalArray = [[NSMutableArray alloc] init];
NSMutableArray *copyArray = [[NSMutableArray alloc] init];

[originalArray addObject:firstString];
[copyArray addObject:firstString];

[originalArray addObject:secondString];
[copyArray addObject:secondString];

[[copyArray objectAtIndex:0] appendString:@"add some text"];

for (int index = 0; index < [originalArray count]; index++) {
    NSLog(@"Original:%@ --- copy:%@", [originalArray objectAtIndex:index], [copyArray objectAtIndex:index]);
}

输出 -

2012-05-09 11:11:18.275 Demo[5433:f803] Original:firstadd some text --- copy:firstadd some text
2012-05-09 11:11:18.277 Demo[5433:f803] Original:second --- copy:second
于 2012-05-09T04:59:23.177 回答