diff --git a/SMModelObject.h b/SMModelObject.h index 20d65e8..d5b2338 100644 --- a/SMModelObject.h +++ b/SMModelObject.h @@ -1,3 +1,38 @@ +/* + The MIT License + + Copyright (c) 2010 Nick Farina + + 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. +*/ + +// SMModelObject is a very handy superclass for "Model Object" types. +// It requires the 64-bit runtime and LLVM Clang >=1.6. + +// Additionally, as of this writing, you'll need to add the compiler flags "-Xclang -fobjc-nonfragile-abi2" in that exact +// order if you don't want to have to write @synthesize yourself. This shouldn't be necessary after the official Clang 2.0 release. + +// Much, much credit given to André Pang and his phenomenal RMModelObject which was the inspiration for this class. +// https://github.com/andrep/RMModelObject @interface SMModelObject : NSObject + ++ (id)make; // equivalent to [[[Class alloc] init] autorelease] + @end diff --git a/SMModelObject.m b/SMModelObject.m index 406263c..7f1bc15 100644 --- a/SMModelObject.m +++ b/SMModelObject.m @@ -3,6 +3,10 @@ @implementation SMModelObject ++ (id)make { + return [[[self alloc] init] autorelease]; +} + - (void)enumerateIvarsUsingBlock:(void (^)(Ivar var, NSString *name, BOOL *cancel))block { BOOL cancel = NO; @@ -87,4 +91,49 @@ - (NSUInteger)hash return hash; } +- (void) writeToDescription:(NSMutableString *)description withIndent:(NSUInteger)indent { + + [description appendFormat:@"<%@ %p", NSStringFromClass([self class]), self]; + + void (^addLineBreak)() = ^ { + [description appendString:@"\n"]; + for (int i=0;i"]; +} + +- (NSString *) description { + NSMutableString *description = [NSMutableString string]; + [self writeToDescription:description withIndent:1]; + return description; +} + @end diff --git a/SampleProject/AppDelegate.m b/SampleProject/AppDelegate.m index fab402b..190bb9c 100644 --- a/SampleProject/AppDelegate.m +++ b/SampleProject/AppDelegate.m @@ -24,19 +24,38 @@ - (void) die { livesRemaining--; } @implementation AppDelegate - (void)applicationDidFinishLaunching:(UIApplication *)application { + + { + NSMutableDictionary *house = [NSMutableDictionary dictionary]; + [house setObject:[NSNumber numberWithUnsignedInteger:5] forKey:@"capacity"]; + + NSMutableDictionary *fluffy = [NSMutableDictionary dictionary]; + [fluffy setObject:@"Fluffy" forKey:@"name"]; + [fluffy setObject:[NSNumber numberWithUnsignedInteger:9] forKey:@"livesRemaining"]; + + NSMutableDictionary *mrWiggles = [NSMutableDictionary dictionary]; + [mrWiggles setObject:@"Mr. Wiggles" forKey:@"name"]; + [mrWiggles setObject:[NSNumber numberWithUnsignedInteger:9] forKey:@"livesRemaining"]; + + [house setObject:[NSArray arrayWithObjects:fluffy,mrWiggles,nil] forKey:@"cats"]; + + NSLog(@"Dict: \n%@", house); + } - House *myHouse = [[[House alloc] init] autorelease]; - myHouse.capacity = 5; - - Cat *fluffy = [[[Cat alloc] init] autorelease]; - fluffy.name = @"Fluffy"; - - Cat *mrWiggles = [[[Cat alloc] init] autorelease]; - mrWiggles.name = @"Mr. Wiggles"; - - myHouse.cats = [NSArray arrayWithObjects:fluffy,mrWiggles,nil]; - - NSLog(@"My House: %@", myHouse); + { + House *house = [House make]; + house.capacity = 5; + + Cat *fluffy = [Cat make]; + fluffy.name = @"Fluffy"; + + Cat *mrWiggles = [Cat make]; + mrWiggles.name = @"Mr. Wiggles"; + + house.cats = [NSArray arrayWithObjects:fluffy,mrWiggles,nil]; + + NSLog(@"My House: \n%@", house); + } } @end