/
AppDelegate.m
93 lines (64 loc) · 2.32 KB
/
AppDelegate.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#import "AppDelegate.h"
#import "SMModelObject.h"
@interface House : SMModelObject
@property (nonatomic, assign) NSUInteger capacity;
@property (nonatomic, copy) NSArray *cats;
@end
@implementation House
@end
@interface Cat : SMModelObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSUInteger livesRemaining;
- (void)die;
@end
@implementation Cat
- (id)init { if (self = [super init]) livesRemaining = 9; return self; }
- (void) die { livesRemaining--; }
@end
@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 *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
/*
Dog *fido = [[Dog alloc] init];
fido.name = @"Fido";
Animal *nick = [[Animal alloc] init];
nick.name = @"Nick";
fido.owner = nick;
fido.lives = 7;
NSLog(@"Copy nick");
Animal *zombieNick = [[nick copy] autorelease];
NSLog(@"Zombie nick's name is %@", zombieNick.name);
NSLog(@"Encode fido");
[NSKeyedArchiver archiveRootObject:fido toFile:@"/test.txt"];
NSLog(@"Decode fido");
Dog *zombieFido = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/test.txt"];
NSLog(@"Zombie dog's name is %@ and he has %i lives.", zombieFido.name, zombieFido.lives);
NSLog(@"Compare fido to zombieFido. Equal? %i. Hash? %i and %i.", [zombieFido isEqual:fido], [zombieFido hash], [fido hash]);
NSLog(@"Release nick.");
[nick release];
NSLog(@"Release fido.");
[fido release];
*/