Skip to content
Browse files

Updated to version 1.1.2

  • Loading branch information...
1 parent 38a1d2f commit d889486b88dcd6f6e0e7fbfcbdcd7f3e0c7321d9 @nicklockwood committed Oct 14, 2012
View
45 Examples/AutoTodoList/AutoCoding/NSObject+AutoCoding.h
@@ -1,45 +0,0 @@
-//
-// NSObject+AutoCoding.h
-//
-// Version 1.1.2
-//
-// Created by Nick Lockwood on 19/11/2011.
-// Copyright (c) 2011 Charcoal Design
-//
-// Distributed under the permissive zlib License
-// Get the latest version from here:
-//
-// https://github.com/nicklockwood/AutoCoding
-//
-// This software is provided 'as-is', without any express or implied
-// warranty. In no event will the authors be held liable for any damages
-// arising from the use of this software.
-//
-// Permission is granted to anyone to use this software for any purpose,
-// including commercial applications, and to alter it and redistribute it
-// freely, subject to the following restrictions:
-//
-// 1. The origin of this software must not be misrepresented; you must not
-// claim that you wrote the original software. If you use this software
-// in a product, an acknowledgment in the product documentation would be
-// appreciated but is not required.
-//
-// 2. Altered source versions must be plainly marked as such, and must not be
-// misrepresented as being the original software.
-//
-// 3. This notice may not be removed or altered from any source distribution.
-//
-
-@interface NSObject (AutoCoding) <NSCoding, NSCopying>
-
-//coding
-
-- (NSArray *)codableKeys;
-- (NSArray *)uncodableKeys;
-
-//loading / saving
-
-+ (instancetype)objectWithContentsOfFile:(NSString *)path;
-- (void)writeToFile:(NSString *)filePath atomically:(BOOL)useAuxiliaryFile;
-
-@end
View
165 Examples/AutoTodoList/AutoCoding/NSObject+AutoCoding.m
@@ -1,165 +0,0 @@
-//
-// NSObject+AutoCoding.m
-//
-// Version 1.1.2
-//
-// Created by Nick Lockwood on 19/11/2011.
-// Copyright (c) 2011 Charcoal Design
-//
-// Distributed under the permissive zlib License
-// Get the latest version from here:
-//
-// https://github.com/nicklockwood/AutoCoding
-//
-// This software is provided 'as-is', without any express or implied
-// warranty. In no event will the authors be held liable for any damages
-// arising from the use of this software.
-//
-// Permission is granted to anyone to use this software for any purpose,
-// including commercial applications, and to alter it and redistribute it
-// freely, subject to the following restrictions:
-//
-// 1. The origin of this software must not be misrepresented; you must not
-// claim that you wrote the original software. If you use this software
-// in a product, an acknowledgment in the product documentation would be
-// appreciated but is not required.
-//
-// 2. Altered source versions must be plainly marked as such, and must not be
-// misrepresented as being the original software.
-//
-// 3. This notice may not be removed or altered from any source distribution.
-//
-
-#import "NSObject+AutoCoding.h"
-#import <objc/runtime.h>
-
-
-@implementation NSObject (AutoCoding)
-
-+ (instancetype)objectWithContentsOfFile:(NSString *)filePath
-{
- //load the file
- NSData *data = [NSData dataWithContentsOfFile:filePath];
-
- //attempt to deserialise data as a plist
- id object = nil;
- if (data)
- {
- NSPropertyListFormat format;
- if ([NSPropertyListSerialization respondsToSelector:@selector(propertyListWithData:options:format:error:)])
- {
- object = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:&format error:NULL];
- }
- else
- {
- object = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:NULL];
- }
-
- //success?
- if (object)
- {
- //check if object is an NSCoded unarchive
- if ([object respondsToSelector:@selector(objectForKey:)] && [object objectForKey:@"$archiver"])
- {
- object = [NSKeyedUnarchiver unarchiveObjectWithData:data];
- }
- }
- else
- {
- //return raw data
- object = data;
- }
- }
-
- //return object
- return object;
-}
-
-- (void)writeToFile:(NSString *)filePath atomically:(BOOL)useAuxiliaryFile
-{
- //note: NSData, NSDictionary and NSArray already implement this method
- //and do not save using NSCoding, however the objectWithContentsOfFile
- //method will correctly recover these objects anyway
-
- //archive object
- NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self];
- [data writeToFile:filePath atomically:useAuxiliaryFile];
-}
-
-- (NSArray *)codableKeys
-{
- NSMutableArray *array = [NSMutableArray array];
- Class class = [self class];
- while (class != [NSObject class])
- {
- unsigned int count;
- objc_property_t *properties = class_copyPropertyList(class, &count);
- for (int i = 0; i < count; i++)
- {
- objc_property_t property = properties[i];
- const char *attributes = property_getAttributes(property);
- NSString *encoding = [NSString stringWithCString:attributes encoding:NSUTF8StringEncoding];
- if (![[encoding componentsSeparatedByString:@","] containsObject:@"R"])
- {
- //omit read-only properties
- const char *name = property_getName(property);
- NSString *key = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
- [array addObject:key];
- }
- }
- free(properties);
- class = [class superclass];
- }
- [array removeObjectsInArray:[self uncodableKeys]];
- return array;
-}
-
-- (NSArray *)uncodableKeys
-{
- return nil;
-}
-
-- (void)setNilValueForKey:(NSString *)key
-{
- //don't throw exception
-}
-
-- (void)setWithCoder:(NSCoder *)aDecoder
-{
- for (NSString *key in [self codableKeys])
- {
- id object = [aDecoder decodeObjectForKey:key];
- [self setValue:object forKey:key];
- }
-}
-
-- (instancetype)initWithCoder:(NSCoder *)aDecoder
-{
- if ((self = [self init]))
- {
- [self setWithCoder:aDecoder];
- }
- return self;
-}
-
-- (void)encodeWithCoder:(NSCoder *)aCoder
-{
- for (NSString *key in [self codableKeys])
- {
- id object = [self valueForKey:key];
- [aCoder encodeObject:object forKey:key];
- }
-}
-
-- (instancetype)copyWithZone:(NSZone *)zone
-{
- NSObject *copy = [[[self class] allocWithZone:zone] init];
- for (NSString *key in [self codableKeys])
- {
- id object = [self valueForKey:key];
- [copy setValue:object forKey:key];
- }
- return copy;
-}
-
-@end
View
2 HRCoder/HRCoder.h
@@ -1,7 +1,7 @@
//
// HRCoder.h
//
-// Version 1.1.1
+// Version 1.1.2
//
// Created by Nick Lockwood on 24/04/2012.
// Copyright (c) 2011 Charcoal Design
View
42 HRCoder/HRCoder.m
@@ -1,7 +1,7 @@
//
// HRCoder.m
//
-// Version 1.1.1
+// Version 1.1.2
//
// Created by Nick Lockwood on 24/04/2012.
// Copyright (c) 2011 Charcoal Design
@@ -234,7 +234,7 @@ - (id)archivedPlistWithRootObject:(id)rootObject
{
[_stack removeAllObjects];
[_knownObjects removeAllObjects];
- [_knownObjects setObject:rootObject forKey:HRCoderRootObjectKey];
+ if (rootObject) [_knownObjects setObject:rootObject forKey:HRCoderRootObjectKey];
id plist = [rootObject archivedObjectWithHRCoder:self];
[_knownObjects removeAllObjects];
[_stack removeAllObjects];
@@ -276,26 +276,30 @@ - (BOOL)containsValueForKey:(NSString *)key
return [[_stack lastObject] objectForKey:key] != nil;
}
-- (id)encodedObject:(id)objv forKey:(NSString *)key
+- (id)encodedObject:(id)object forKey:(NSString *)key
{
- NSInteger knownIndex = [[_knownObjects allValues] indexOfObject:objv];
- if (knownIndex != NSNotFound)
- {
- //create alias
- NSString *aliasKeyPath = [[_knownObjects allKeys] objectAtIndex:knownIndex];
- NSDictionary *alias = [NSDictionary dictionaryWithObject:aliasKeyPath forKey:HRCoderObjectAliasKey];
- return alias;
- }
- else
+ if (object && key)
{
- //encode object
- NSString *oldKeyPath = _keyPath;
- self.keyPath = _keyPath? [_keyPath stringByAppendingPathExtension:key]: key;
- [_knownObjects setObject:objv forKey:_keyPath];
- id encodedObject = [objv archivedObjectWithHRCoder:self];
- self.keyPath = oldKeyPath;
- return encodedObject;
+ NSInteger knownIndex = [[_knownObjects allValues] indexOfObject:object];
+ if (knownIndex != NSNotFound)
+ {
+ //create alias
+ NSString *aliasKeyPath = [[_knownObjects allKeys] objectAtIndex:knownIndex];
+ NSDictionary *alias = [NSDictionary dictionaryWithObject:aliasKeyPath forKey:HRCoderObjectAliasKey];
+ return alias;
+ }
+ else
+ {
+ //encode object
+ NSString *oldKeyPath = _keyPath;
+ self.keyPath = _keyPath? [_keyPath stringByAppendingPathExtension:key]: key;
+ [_knownObjects setObject:object forKey:_keyPath];
+ id encodedObject = [object archivedObjectWithHRCoder:self];
+ self.keyPath = oldKeyPath;
+ return encodedObject;
+ }
}
+ return nil;
}
- (void)encodeObject:(id)objv forKey:(NSString *)key
View
2 LICENCE.md
@@ -1,6 +1,6 @@
HRCoder
-Version 1.1.1, October 11th, 2012
+Version 1.1.2, October 14th, 2012
Copyright (C) 2012 Charcoal Design
View
4 RELEASE NOTES.md
@@ -1,3 +1,7 @@
+Version 1.1.2
+
+- Fixed crash when attempting to archive a nil object property
+
Version 1.1.1
- HRCoder now calls the `awakeAfterUsingCoder:` method on objects after they are unarchived.

0 comments on commit d889486

Please sign in to comment.
Something went wrong with that request. Please try again.