Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added license, and very useful -description method.
  • Loading branch information
nfarina committed Nov 30, 2010
1 parent 8815358 commit 870cb80
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 12 deletions.
35 changes: 35 additions & 0 deletions 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<NSCoding,NSCopying>

+ (id)make; // equivalent to [[[Class alloc] init] autorelease]

@end
49 changes: 49 additions & 0 deletions SMModelObject.m
Expand Up @@ -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;
Expand Down Expand Up @@ -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<indent;i++) [description appendString:@"\t"];
};

[self enumerateIvarsUsingBlock:^(Ivar var, NSString *name, BOOL *cancel) {

addLineBreak();
id object = [self valueForKey:name];

if ([object isKindOfClass:[SMModelObject class]]) {
[object writeToDescription:description withIndent:indent+1];
}
else if ([object isKindOfClass:[NSArray class]]) {

[description appendFormat:@"%@ =", name];

for (id child in object) {
addLineBreak();
[description appendString:@"\t"];

if ([child isKindOfClass:[SMModelObject class]])
[child writeToDescription:description withIndent:indent+2];
else
[description appendString:[child description]];
}
}
else {
[description appendFormat:@"%@ = %@", name, object];
}
}];

[description appendString:@">"];
}

- (NSString *) description {
NSMutableString *description = [NSMutableString string];
[self writeToDescription:description withIndent:1];
return description;
}

@end
43 changes: 31 additions & 12 deletions SampleProject/AppDelegate.m
Expand Up @@ -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
Expand Down

0 comments on commit 870cb80

Please sign in to comment.