Skip to content

An Objective-C category that enables automatic copying of any object

License

Notifications You must be signed in to change notification settings

JohnSundell/JSAutoCopy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JSAutoCopy

Automatically copy any Objective-C object

  • No need to write manual NSCopying implementations, just use the methods provided by NSObject+JSAutoCopy to automatically copy your objects.
  • Manual -copyWithZone: implementations can quickly become outdated as your class' interface is changed, and can therefor cause hard-to-find bugs & issues.
  • Provides several override points for custom copying on a property basis.

Here's how to use JSAutoCopy:

1. Import NSObject+JSAutoCopy
#import "NSObject+JSAutoCopy.h"
2. Use any of the category's public methods to create an automatic copy of an object
// Copy all properties of an object
MyClass *myCopy = [myObject autoCopy];

// Ignore certain properties when copying
NSArray *ignoredPropertyNames = @[@"ignoredPropertyName"];
MyClass *myOtherCopy = [myObject autoCopyIgnoringProperties:ignoredPropertyNames];

// Invoke a block for each property when copying
MyClass *myThirdCopy = [myObject autoCopyWithBlock:^id(NSString *propertyName, id autoCopiedValue) {
	if ([propertyName isEqualToString:@"mySpecialProperty"]) {
		return [autoCopiedValue doSomethingBeforeCopying];
	}

	return autoCopiedValue;
}];

// Call back to a delegate for each property when copying
MyClass *myForthCopy = [myObject autoCopyWithDelegate:self];

- (id)autoCopiedValueForObject:(id)object propertyName:(NSString *)propertyName autoCopiedValue:(id)autoCopiedValue
{
	if ([propertyName isEqualToString:@"mySpecialProperty"]) {
		return [autoCopiedValue doSomethingBeforeCopying];
	}

	return autoCopiedValue;
}

Hope that you'll enjoy using JSAutoCopy!

Why not give me a shout on Twitter: @johnsundell

About

An Objective-C category that enables automatic copying of any object

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published