IOS object replication tool

Time to tidy up some gadgets.


There are times when we might need to operate on a Model without changing the source data.
can think of it at this time

1, New a new object, and then assign.

2, the realization of < NSCopying> agreement. In fact, they are copied one by one.

If the object is more complex, or more objects to operate. It’s a time-consuming thing. So I encapsulate the following automatically copied classes: AutoCopy.
download address: https://github.com/anjohnlv/AutoCopy

Usage:
1, Download NSObject+Copy.h and NSObject+Copy.m, and import the project.
2 introduces the head #import “NSObject+Copy.h”
3 in the place where it needs to float, and calls the autoCopy method to the object that needs to be copied: Person *clone = [person autoCopy];

This widget does not support objects such as NSArray, NSDictionary, etc. these objects can be called directly to mutableCopy.


Say down principle: get all the property names of the class at run time, and pass the value from the old model to the new model by kvc.

- (NSArray * allPropertyName) {unsigned int propertyCount = 0; objc_property_t = *properties class_copyPropertyList ([self class], & propertyCount); NSMutableArray *allPropertyName = [NSMutableArray new]; for (unsigned int i = 0; I < propertyCount; ++i) {objc_property_t property = properties[i]; NSString = *propertyName [NSString stringWithCString:property_getName (property) encoding:NSUTF8StringEncoding]; [allPropertyName addObject:propertyName];} free (properties); return allPropertyName;}
- (instancetype) autoCopy if (self) {{ID copyObject = [[self class] new]; NSArray *allPropertyName = [self allPropertyName]; for (int i=0; i< [allPropertyName; count]; i++) {NSString *propertyName = allPropertyName[i] [copyObject setValue:[self valueForKey:propertyName] forKey:propertyName]; return copyObject;}}; return self;}

What if there is a wrong place, welcome back.