Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
language: objective-c
2 changes: 1 addition & 1 deletion Archiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@interface Archiver : NSObject {

}

+ (id)retrieve:(NSString *)key;
Expand Down
4 changes: 2 additions & 2 deletions Archiver.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ + (id)retrieve:(NSString *)key {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
return [[[NSKeyedUnarchiver unarchiveObjectWithFile:filePath] retain] autorelease];
return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

+ (BOOL)persist:(id)object key:(NSString *)key {
Expand All @@ -29,7 +29,7 @@ + (BOOL)delete:(NSString *)key {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:[NSString stringWithFormat:@"/%@.archive", key]];
return [fileManager removeItemAtPath:filePath error:NULL];
return [fileManager removeItemAtPath:filePath error:NULL];
}

+ (BOOL)deleteEverything {
Expand Down
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2013 NSObject-NSCoding-Kingiol

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
53 changes: 20 additions & 33 deletions NSObject+NSCoding.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ @implementation NSObject (NSCoding)

- (NSMutableDictionary *)propertiesForClass:(Class)klass {

NSMutableDictionary *results = [[[NSMutableDictionary alloc] init] autorelease];
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];

unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
Expand Down Expand Up @@ -68,7 +68,10 @@ - (void)autoEncodeWithCoder:(NSCoder *)coder {
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
value = [self performSelector:NSSelectorFromString(key)];
#pragma clang diagnostic pop

// only decode if the property conforms to NSCoding
if([class conformsToProtocol:@protocol(NSCoding)]){
Expand Down Expand Up @@ -123,7 +126,7 @@ - (void)autoEncodeWithCoder:(NSCoder *)coder {
break;
default:
break;
}
}
}
}

Expand All @@ -133,7 +136,6 @@ - (void)autoDecode:(NSCoder *)coder {
NSString *type = [properties objectForKey:key];
id value;
NSNumber *number;
unsigned int addr;
NSInteger i;
CGFloat f;
BOOL b;
Expand All @@ -143,81 +145,66 @@ - (void)autoDecode:(NSCoder *)coder {
long longValue;
unsigned unsignedValue;
short shortValue;
Ivar ivar;
double *varIndex;

NSString *className;

switch ([type characterAtIndex:0]) {
case '@': // object
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
// only decode if the property conforms to NSCoding
if ([class conformsToProtocol:@protocol(NSCoding )]){
value = [[coder decodeObjectForKey:key] retain];
addr = (NSInteger)&value;
object_setInstanceVariable(self, [key UTF8String], *(id**)addr);
value = [coder decodeObjectForKey:key];
[self setValue:value forKey:key];
}
}
break;
case 'c': // bool
number = [coder decodeObjectForKey:key];
number = [coder decodeObjectForKey:key];
b = [number boolValue];
addr = (NSInteger)&b;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(b) forKey:key];
break;
case 'f': // float
number = [coder decodeObjectForKey:key];
number = [coder decodeObjectForKey:key];
f = [number floatValue];
addr = (NSInteger)&f;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(f) forKey:key];
break;
case 'd': // double
case 'd': // double
number = [coder decodeObjectForKey:key];
d = [number doubleValue];
if ((ivar = class_getInstanceVariable([self class], [key UTF8String]))) {
varIndex = (double *)(void **)((char *)self + ivar_getOffset(ivar));
*varIndex = d;
}
[self setValue:@(d) forKey:key];
break;
case 'i': // int
number = [coder decodeObjectForKey:key];
i = [number intValue];
addr = (NSInteger)&i;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(i) forKey:key];
break;
case 'L': // unsigned long
number = [coder decodeObjectForKey:key];
ul = [number unsignedLongValue];
addr = (NSInteger)&ul;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(ul) forKey:key];
break;
case 'Q': // unsigned long long
number = [coder decodeObjectForKey:key];
ull = [number unsignedLongLongValue];
addr = (NSInteger)&ull;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(ull) forKey:key];
break;
case 'l': // long
number = [coder decodeObjectForKey:key];
longValue = [number longValue];
addr = (NSInteger)&longValue;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(longValue) forKey:key];
break;
case 'I': // unsigned
number = [coder decodeObjectForKey:key];
unsignedValue = [number unsignedIntValue];
addr = (NSInteger)&unsignedValue;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(unsignedValue) forKey:key];
break;
case 's': // short
number = [coder decodeObjectForKey:key];
shortValue = [number shortValue];
addr = (NSInteger)&shortValue;
object_setInstanceVariable(self, [key UTF8String], *(NSInteger**)addr);
[self setValue:@(shortValue) forKey:key];
break;

default:
break;
}
Expand Down
12 changes: 12 additions & 0 deletions NSObject-NSCoding.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Pod::Spec.new do |s|
s.name = "NSObject-NSCoding"
s.version = "1.0"
s.summary = "Automatic NSCoding and persistence implementation"
s.homepage = "https://github.com/kingiol/NSObject-NSCoding"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "greenisus" => "[email protected]", "kingiol" => "[email protected]" }
s.platform = :ios
s.source = { :git => "https://github.com/kingiol/NSObject-NSCoding.git", :tag => "1.0" }
s.source_files = '*.{h,m}'
s.requires_arc = true
end
60 changes: 0 additions & 60 deletions README

This file was deleted.

77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
NSObject+NSCoding and Archiver
------------------------------

build by Kingiol

[![Build Status](https://travis-ci.org/kingiol/NSObject-NSCoding.png?branch=master)](https://travis-ci.org/kingiol/NSObject-NSCoding)

Mike Mayo
Rackspace Mobile Apps
[email protected]
twitter: @greenisus

These are some simple classes to make object persistence with NSCoding easier. This code was extracted from
the Rackspace Cloud / OpenStack iOS app at http://launchpad.net/openstack-ios

# Installation

CocoaPods
-

NSObject-NSCoding is easiest to install using [CocoaPods](http://cocoapods.org).
Simply add this line to your Podfile

pod 'NSObject-NSCoding'

and run `pod install`

Static
-

If you prefer not to use CocoaPods, simply drag `Archiver.h`, `Archiver.m`, `NSObject+NSCoding.h`, and `NSObject+NSCoding.m` into your project.

Then, right click `Frameworks` in `Groups & Files` and choose `Add` -> `Existing Frameworks...` and choose `libobjc.A.dylib`.

# Usage

Archiver
-

This class will read and write objects that conform to the NSCoding protocol to disk.

SomeClass *myObject = [[[SomeClass alloc] init] autorelease];
myObject.someProperty = @"Hello world";

[Archiver persist:myObject key:@"myObject"];

// later on somewhere else...

SomeClass *myObject = [Archiver retrieve:@"myObject"];

NSObject+NSCoding
-

This category simplifies implementing NSCoding by iterating over the properties of your
class and encoding/decoding them for you. It persists primitives (such as ints and floats)
as well as any objects that conform to NSCoding.

In your class header, conform to NSCoding:

@interface Model : NSObject <NSCoding>

In your class implementation, call the automatic methods:

- (void)encodeWithCoder:(NSCoder *)coder {
[self autoEncodeWithCoder:coder];
}

- (id)initWithCoder:(NSCoder *)coder {
if (self = [super init]) {
[self autoDecode:coder];
}
return self;
}

# License

NSObject+NSCoding is released under the MIT license. A full description of that license is available in the LICENSE file.