Giter VIP home page Giter VIP logo

jsautoencodedobject's Introduction

JSAutoEncodedObject

Automatically encode or decode any Objective-C object

  • No need to write manual NSCoding implementations, just inherit from JSAutoEncodedObject and all NSData encoding and decoding will be done automatically for your class.
  • Provides an override point, to let you specify any properties you don't want to encode for a specific class.
  • Super-useful when working with Apple's NSData-based APIs, such as NSUserDefaults or GameCenter.

Here's how to use JSAutoEncodedObject:

1. Inherit from JSAutoEncodedObject
#import "JSAutoEncodedObject.h"

@interface MyClass : JSAutoEncodedObject

@end
2. Optional: Provide a schema that JSAutoEncodedObject will use when encoding/decoding your objects
+ (JSAutoEncodedObjectSchema *)schema
{
	return [JSAutoEncodedObjectSchema schemaFromDictionary:@{
		@"property1": @"p1",
		@"property2": @"p2"
	}];
}
3. Optional: Override -encodedValueForPropertyNamed: and/or -setValueForPropertyNamed:toDecodedValue: to add custom encoding/decoding to a specific property
- (id)encodedValueForPropertyNamed:(NSString *)propertyName
{
	if ([propertyName isEqualToString:@"specialProperty"]) {
		return [self.specialProperty stringByAppendingString:@"aCustomSuffix"];
	}

	return [super encodedValueForPropertyNamed:propertyName];
}

- (void)setValueForPropertyNamed:(NSString *)propertyName toDecodedValue:(id)decodedValue
{
	if ([propertyName isEqualToString:@"specialProperty"]) {
		NSString *decodedString = [decodedValue stringByReplacingOccurrencesOfString:@"aCustomSuffix" withString:@""];
		[super setValueForPropertyNamed:propertyName toDecodedValue:decodedString];
		return;
	}

	[super setValueForPropertyNamed:propertyName toDecodedValue:decodedValue];
}
4. Done! You can now encode or decode instaces of your class and let JSAutoEncodedObject take care of the rest
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myClassInstance];

Parse-support

JSAutoEncodedObject also comes with Parse-support for converting your model objects to/from Parse's PFObject (see www.parse.com for more info). Parse-support is provided through the JSAutoEncodedObject+Prase category.

Hope that you'll enjoy using JSAutoEncodedObject!

Why not give me a shout on Twitter: @johnsundell

jsautoencodedobject's People

Contributors

johnsundell avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.