来自另一个的Init数组,无需修改其内容

时间:2014-03-25 14:35:19

标签: ios objective-c copy nsmutablearray nsarray

我需要将一个数组“克隆”到另一个数组,但问题是当我修改复制的数组时,原始数据也会被修改。使用硬拷贝无法正常工作。

我正在初始化一个这样的数组:

NSMutableArray *otherArray = [[NSMutableArray alloc] initWithArray: myList copyItems:YES];

其中myList是NSArray,作为我函数中的参数。 问题是当我需要返回myList时,我的内容在我修改otherArray

时被修改了

我尝试制作类似的硬拷贝:

NSMutableArray* algo = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:myList]];

但是,某些属性未转换,并且为它们分配了一个nil值。

我也试过这个:

NSMutableArray *otherArray = [myList mutableCopy];

嗯,显而易见的问题是:如何在不修改原始对象的情况下修改复制的对象?

谢谢!

编辑:这是我的整个功能。

    RAC(self,filteredPacks) = [RACSignal combineLatest:@[self.searchBoxSignal, self.packListsSignal]
                                                   reduce:^NSArray *(NSString *filterString, NSArray *packList) {

                                                       NSMutableArray *sweetHelper = [[NSMutableArray alloc] init];
                                                       NSMutableArray* packListCopy = [NSKeyedUnarchiver unarchiveObjectWithData:
                                                                               [NSKeyedArchiver archivedDataWithRootObject:packList]];

                                                       filterString = [filterString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

                                                       if ([filterString length] > 0)
                                                       {
                                                          for(PackList *theList in packListCopy){
                                                              NSMutableIndexSet *indexesToDelete = [NSMutableIndexSet indexSet];
                                                               NSUInteger currentIndex = 0;
                                                               for(Pack *thePack in theList.resolved_packs){

                                                                   if([thePack.name rangeOfString:filterString options:NSCaseInsensitiveSearch].location == NSNotFound){
                                                                       [indexesToDelete addIndex:currentIndex];
                                                                   }
                                                                   currentIndex++;
                                                                 }
                                                               [theList.resolved_packs removeObjectsAtIndexes:indexesToDelete];
                                                               [theList.packs removeObjectsAtIndexes:indexesToDelete];

                                                               [sweetHelper addObject:theList];
                                                           }
                                                           return sweetHelper;
                                                       }
                                                       else
                                                       {
                                                           return self.originalList;
                                                       }
                                                   }
                                  ];

1 个答案:

答案 0 :(得分:0)

好吧,我在同一个问题中找到了使用此answer和此comment的解决方案。

正如我在问题中所说:

“我试着制作一个像以下那样的硬拷贝:

NSMutableArray* algo = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:myList]];

但是,某些属性未转换,并且为它们分配了一个nil值。“

那么,未转换的属性,它们不是因为它们是自定义对象,我没有实现initWithCode和encodeWithCoder方法。所以,当我将这些方法添加到我的customObjectClass时,我的Arrays被复制了所有元素。

关于我做了什么的一个小例子:

在我的SomeCustomObject.h中,我应该实现NSCoding:

@interface SomeCustomObject : NSObject <NSCoding> {
  NSMutableArray * __packs;
  int __type;
  Link * __selfRef;
  NSMutableArray * __resolved_packs;
}

在我的SomeCustomObject的子类中,我有类似的东西:

- (id)initWithCoder:(NSCoder *)decoder {
 if (self = [super init]) {
    __packs = [decoder decodeObjectForKey:@"packs"];
    __type = [decoder decodeIntForKey:@"type"];
    __selfRef = [decoder decodeObjectForKey:@"selfRef"];
    __resolved_packs = [decoder decodeObjectForKey:@"resolved_packs"];
 }
 return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:__packs forKey:@"packs"];
  [encoder encodeInt:__type forKey:@"type"];
  [encoder encodeObject:__selfRef forKey:@"selfRef"];
  [encoder encodeObject:__resolved_packs forKey:@"resolved_packs"];
}

我希望这对某人有用:)