[英]How to copy custom objects in Objective-C?
I have two arrays. 我有两个数组。 First contains custom objects.
首先包含自定义对象。 Now I want to copy all object of first array in another array.
现在我想将第一个数组的所有对象复制到另一个数组中。 For that I am using below code.
为此,我使用下面的代码。
Arays. 一阳。
arr_post=[[NSMutableArray alloc]init];
copy_arr_user_post=[[NSMutableArray alloc]init];
am adding the objects into them like this. 正在像这样向其中添加对象。
for(i=0;i<[arr_main count];i++)
{
Post *obj=[[Post alloc]init];
obj.name=@"abc";
obj.category=@"social";
[arr_post addObject:obj];
}
Now I am copying to another array like this 现在我正在复制到另一个这样的数组
[arr_post addObject:user_post];
Post *objectCopy = [user_post copy]; //create a copy of our object
[copy_arr_user_post addObject: objectCopy]; //insert copy into other array
In Post.h 在Post.h中
@interface Post : NSObject<NSCopying>
In Post.m 在Post.m
- (id)copyWithZone:(NSZone *)zone
{
// Copying code here.
Post *another =[[[self class] allocWithZone:zone] init];
another.id=self.id;
another.category=self.category;
return another;
}
But it does not copy objects I get null value. 但是它不会复制我得到空值的对象。 Why?
为什么?
One method that I find faster than NSCopyng 我发现一种比NSCopyng更快的方法
-Create an NSObject category with this two method -使用这两种方法创建一个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;
}
-Make your object conforms to NSCoding -使您的对象符合NSCoding
#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];
}
}
-Import the category -导入类别
Now you are able to copy any kinds of object 现在您可以复制任何类型的对象
MYObject *copy = [originalObject deepCopy];
NSArray *arrayWithCopiedObjects = [originalArray deepCopy];
etc.... 等等....
Try 尝试
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);
And log 并登录
2015-10-23 13:25:31.994 OCTest[1784:130931] (
"123 social",
"abc social",
"abc social"
)
2015-10-23 13:25:31.995 OCTest[1784:130931] (
"abc social",
"abc social",
"abc social"
)
I add description
for debugging 我添加调试
description
-(NSString *)description{
return [NSString stringWithFormat:@"%@ %@",self.name,self.category];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.