Ios 如何在Objective-C中复制自定义对象?
我有两个数组。第一个包含自定义对象。现在我想将第一个数组的所有对象复制到另一个数组中。为此,我使用下面的代码 Arays.Ios 如何在Objective-C中复制自定义对象?,ios,objective-c,arrays,Ios,Objective C,Arrays,我有两个数组。第一个包含自定义对象。现在我想将第一个数组的所有对象复制到另一个数组中。为此,我使用下面的代码 Arays. arr_post=[[NSMutableArray alloc]init]; copy_arr_user_post=[[NSMutableArray alloc]init]; 我像这样将对象添加到它们中 for(i=0;i<[arr_main count];i++) { Post *obj=[[Post alloc]init]; obj.name=@
arr_post=[[NSMutableArray alloc]init];
copy_arr_user_post=[[NSMutableArray alloc]init];
我像这样将对象添加到它们中
for(i=0;i<[arr_main count];i++)
{
Post *obj=[[Post alloc]init];
obj.name=@"abc";
obj.category=@"social";
[arr_post addObject:obj];
}
在Post.h中
@interface Post : NSObject<NSCopying>
但它不复制对象,我得到空值。为什么?试试看
NSMutableArray * arr_post=[[NSMutableArray alloc]init];
NSMutableArray * copy_arr_user_post=[[NSMutableArray alloc]init];
for(int i=0;i<3;i++)
{
Post *obj=[[Post alloc]init];
obj.name=@"abc";
obj.category=@"social";
[arr_post addObject:obj];
}
[arr_post enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[copy_arr_user_post addObject:[obj copy]];
}];
Post * temp = [arr_post objectAtIndex:0];
temp.name = @"123";
NSLog(@"%@",arr_post);
NSLog(@"%@",copy_arr_user_post);
我为调试添加了说明
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@",self.name,self.category];
}
我发现一种比NSCopyng更快的方法 -使用这两种方法创建NSObject类别
#import <objc/runtime.h>
-(id)deepCopy
{
NSArray *tmpArray = @[self];
NSData *buffer = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
return [NSKeyedUnarchiver unarchiveObjectWithData:buffer][0];
}
- (NSMutableArray *)allProperties
{
NSMutableArray *props = [NSMutableArray array];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
//Excluding all readOnly properties
unsigned int numOfAttributes;
objc_property_attribute_t *propertyAttributes = property_copyAttributeList(property, &numOfAttributes);
BOOL foundReadonly = NO;
for ( unsigned int ai = 0; ai < numOfAttributes; ai++ )
{
switch (propertyAttributes[ai].name[0]) {
case 'T': // type
break;
case 'R': // readonly
foundReadonly = YES;
break;
case 'C': // copy
break;
case '&': // retain
break;
case 'N': // nonatomic
break;
case 'G': // custom getter
break;
case 'S': // custom setter
break;
case 'D': // dynamic
break;
default:
break;
}
}
free(propertyAttributes);
if (!foundReadonly)
{
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSASCIIStringEncoding];
[props addObject:propertyName];
}
}
free(properties);
return props;
}
-导入类别
现在您可以复制任何类型的对象
MYObject *copy = [originalObject deepCopy];
NSArray *arrayWithCopiedObjects = [originalArray deepCopy];
等等……澄清您的问题,在发布的代码中,您在哪里获得了
nil
值?哪些代码没有按预期工作?我认为这是研究深度复制集合的一个非常有用的链接:当我从第二个数组中获取对象时,我得到的值为零。你不能在数组中存储nil
对象,因此数组本身必须是nil
“现在你可以复制任何类型的对象了”-否。现在您可以复制任何对象及其属性。这仅适用于只需要复制特性的简单对象。我有很多类不适合这样做。这也不会递归地进行深度复制。
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@",self.name,self.category];
}
#import <objc/runtime.h>
-(id)deepCopy
{
NSArray *tmpArray = @[self];
NSData *buffer = [NSKeyedArchiver archivedDataWithRootObject:tmpArray];
return [NSKeyedUnarchiver unarchiveObjectWithData:buffer][0];
}
- (NSMutableArray *)allProperties
{
NSMutableArray *props = [NSMutableArray array];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
//Excluding all readOnly properties
unsigned int numOfAttributes;
objc_property_attribute_t *propertyAttributes = property_copyAttributeList(property, &numOfAttributes);
BOOL foundReadonly = NO;
for ( unsigned int ai = 0; ai < numOfAttributes; ai++ )
{
switch (propertyAttributes[ai].name[0]) {
case 'T': // type
break;
case 'R': // readonly
foundReadonly = YES;
break;
case 'C': // copy
break;
case '&': // retain
break;
case 'N': // nonatomic
break;
case 'G': // custom getter
break;
case 'S': // custom setter
break;
case 'D': // dynamic
break;
default:
break;
}
}
free(propertyAttributes);
if (!foundReadonly)
{
NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSASCIIStringEncoding];
[props addObject:propertyName];
}
}
free(properties);
return props;
}
#pragma mark - NSCoding
- (instancetype)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (self)
{
NSArray *keys = [self allProperties];
for (NSString *key in keys)
{
[self setValue:[decoder decodeObjectForKey:key] forKey:key] ;
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
NSArray *keys = [self allProperties];
for (NSString *key in keys)
{
[aCoder encodeObject:[self valueForKey:key] forKey:key];
}
}
MYObject *copy = [originalObject deepCopy];
NSArray *arrayWithCopiedObjects = [originalArray deepCopy];