Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

More examples.

  • Loading branch information...
commit 282d78fced6fed93e2b2c9628e35bdfd5746c4bc 1 parent 156a91d
David Nolen authored November 02, 2009

Showing 1 changed file with 65 additions and 7 deletions. Show diff stats Hide diff stats

  1. 72  DataStructures/DataStructures.m
72  DataStructures/DataStructures.m
@@ -4,11 +4,15 @@ int main (int argc, const char * argv[]) {
4 4
 	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
5 5
 	
6 6
 	// ====================
7  
-	// Dictinaries
  7
+	// Dictionaries
8 8
 	// ====================
9  
-	
10  
-	NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"foo", @"bar", nil];
11  
-	NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"foo", @"bar", nil];
  9
+
  10
+	NSLog(@"----------------------");
  11
+	NSLog(@"Dictionaries");
  12
+	NSLog(@"----------------------");
  13
+
  14
+	NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", nil];
  15
+	NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", nil];
12 16
 	
13 17
 	@try {
14 18
 		[dict1 setValue:@"baz" forKey:@"foo"];
@@ -25,6 +29,10 @@ int main (int argc, const char * argv[]) {
25 29
 	// Arrays
26 30
 	// ====================
27 31
 	
  32
+	NSLog(@"----------------------");
  33
+	NSLog(@"Arrays");
  34
+	NSLog(@"----------------------");
  35
+
28 36
 	NSArray *ary1 = [NSArray arrayWithObjects:@"item1", [NSDate date], dict2, nil];
29 37
 	NSMutableArray *ary2 = [NSMutableArray arrayWithObjects:@"item1", [NSDate date], dict2, nil];
30 38
 	
@@ -38,22 +46,72 @@ int main (int argc, const char * argv[]) {
38 46
 	[ary2 removeObjectAtIndex:0];
39 47
 	
40 48
 	// ====================
  49
+	// Sets
  50
+	// ====================
  51
+	
  52
+	NSLog(@"----------------------");
  53
+	NSLog(@"Sets");
  54
+	NSLog(@"----------------------");
  55
+	
  56
+	NSArray *ary5 = [NSArray arrayWithObjects:@"bird", @"dog", @"cat", @"bird", @"zebra", @"lion", nil];
  57
+	NSArray *ary6 = [NSArray arrayWithObjects:@"bird", @"dog", @"cat", @"bird", @"zebra", @"lion", nil];
  58
+	NSSet *set1 = [NSSet setWithArray:ary5];
  59
+	
  60
+	NSLog(@"%@", set1);
  61
+	
  62
+	// ====================
  63
+	// Equality
  64
+	// ====================
  65
+	
  66
+	NSLog(@"----------------------");
  67
+	NSLog(@"Equality");
  68
+	NSLog(@"----------------------");
  69
+
  70
+	// BOOL Objective-C type
  71
+	// YES == 1
  72
+	// NO == 0
  73
+	NSLog(@"ary5 == ary6 %d", [ary5 isEqual:ary6]);
  74
+		
  75
+	// ====================
  76
+	// Arbitrary Keys
  77
+	// ====================
  78
+
  79
+	NSLog(@"----------------------");
  80
+	NSLog(@"Arbitrary Keys");
  81
+	NSLog(@"----------------------");
  82
+	
  83
+	NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"complex key", set1, nil];
  84
+	NSSet *set2 = [NSSet setWithObjects:@"lion", @"dog", @"cat", @"bird", @"bird", @"zebra", nil];
  85
+
  86
+	NSLog(@"%@", [dict3 objectForKey:set2]);
  87
+	
  88
+	// ====================
41 89
 	// Iteration
42 90
 	// ====================
  91
+
  92
+	NSLog(@"----------------------");
  93
+	NSLog(@"Iteration");
  94
+	NSLog(@"----------------------");
43 95
 	
44 96
 	NSArray *ary3 = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", @"five", @"size", @"seven", @"eight", @"nine", @"ten", nil];
45 97
 	
46 98
 	// old school
47 99
 	for(int i = 0; i < [ary3 count]; i++) {
48  
-		NSLog(@"%@", [ary3 objectAtIndex:i]);
  100
+		//NSLog(@"%@", [ary3 objectAtIndex:i]);
49 101
 	}
50 102
 	
51 103
 	// new school
52 104
 	for(NSString *str in ary3) {
53  
-		NSLog(@"%@", str);
  105
+		//NSLog(@"%@", str);
54 106
 	}
55 107
 	
56  
-	
  108
+	// dictionary enumeration
  109
+	NSArray *keys = [NSArray arrayWithObjects:@"first", @"middle", @"last", nil];
  110
+	NSArray *values = [NSArray arrayWithObjects:@"Bob", @"Howard", @"Smith", nil];
  111
+	NSDictionary *dict4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
  112
+	for(id key in dict4) {
  113
+		NSLog(@"%@ = %@", key, [dict4 objectForKey:key]);
  114
+	}
57 115
 	
58 116
 	[pool drain];
59 117
 	return 0;

0 notes on commit 282d78f

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