Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding ignoreNil option

  • Loading branch information...
commit e65768b035443d0d8e531e85d0a48944d9862759 1 parent 1229348
Gabriel Handford authored June 05, 2011
1  .gitignore
@@ -5,3 +5,4 @@ build
5 5
 xcuserdata
6 6
 Documentation/*
7 7
 GHKit.docset
  8
+GHKitIPhone.xcodeproj/project.xcworkspace/xcuserdata
34  Classes/GHNSDictionary+NSNull.h
@@ -38,10 +38,42 @@
38 38
 	[NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNil:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
39 39
  
40 40
  @param firstObject... Alternating key, value pairs. Terminated when _key_ is nil. 
  41
+ @result Dictionary
41 42
  */
42 43
 + (id)gh_dictionaryWithKeysAndObjectsMaybeNil:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
43 44
 
44  
-+ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args;
  45
+/*!
  46
+ Create dictionary which supports nil values.
  47
+ Key is first (instead of value then key). If the value is nil it is stored internally as NSNull,
  48
+ and when calling objectMaybeNilForKey will return nil.
  49
+ 
  50
+ For example,
  51
+ @code
  52
+ - (void)setDictionary:(id)key, ... {
  53
+   va_list args;
  54
+   va_start(args, key);
  55
+   [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:key args:args ignoreNilValues:NO];
  56
+   va_end(args);
  57
+ }
  58
+ @endcode
  59
+ 
  60
+ @param firstKey First key
  61
+ @param args Args va_list via va_start
  62
+ @param ignoreNil If YES, will not store key, value paries where the value is nil
  63
+ @result Dictionary
  64
+ */
  65
++ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args ignoreNil:(BOOL)ignoreNil;
  66
+
  67
+/*!
  68
+ Create dictionary which supports nil values.
  69
+ Key is first (instead of value then key). If the value is nil it is stored internally as NSNull,
  70
+ and when calling objectMaybeNilForKey will return nil.
  71
+ 
  72
+ For example,
  73
+ [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNil:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
  74
+ 
  75
+ @param firstObject... Alternating key, value pairs. Terminated when _key_ is nil. 
  76
+ */
45 77
 
46 78
 /*!
47 79
  Use this method instead of objectForKey if you want nil (and not the internal NSNull).
10  Classes/GHNSDictionary+NSNull.m
@@ -28,7 +28,7 @@
28 28
 
29 29
 @implementation NSDictionary(GHNSNull)
30 30
 
31  
-+ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args {
  31
++ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args ignoreNil:(BOOL)ignoreNil {
32 32
 	if (!firstKey) return [self dictionary];
33 33
 	
34 34
 	NSMutableArray *keys = [[NSMutableArray alloc] init];
@@ -36,10 +36,14 @@ + (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)
36 36
 	id key = firstKey;	
37 37
 	do {
38 38
 		id value = va_arg(args, id);
  39
+    if (!value && ignoreNil) {
  40
+      key = va_arg(args, id);
  41
+      continue;
  42
+    }
39 43
 		if (!value) value = [NSNull null];
40 44
 		[keys addObject:key];
41 45
 		[values addObject:value];
42  
-		key = va_arg(args,id);
  46
+		key = va_arg(args, id);
43 47
 	} while(key);
44 48
 	NSDictionary *dict = [self dictionaryWithObjects:values forKeys:keys];
45 49
 	[keys release];
@@ -50,7 +54,7 @@ + (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)
50 54
 + (id)gh_dictionaryWithKeysAndObjectsMaybeNil:(id)firstKey, ... {	
51 55
 	va_list args;
52 56
   va_start(args, firstKey);
53  
-	NSDictionary *dict = [self gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args];
  57
+	NSDictionary *dict = [self gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args ignoreNil:NO];
54 58
 	va_end(args);
55 59
 	return dict;
56 60
 }
4  GHKit.xcodeproj/project.pbxproj
@@ -459,10 +459,10 @@
459 459
 				52857C920E1997B50011DA5C /* GHNSFileManager+Utils.m */,
460 460
 				52857C950E1997B50011DA5C /* GHNSString+HMAC.h */,
461 461
 				52857C960E1997B50011DA5C /* GHNSString+HMAC.m */,
462  
-				52A425230E1C2F1F0092797C /* hmac.c */,
463 462
 				52A425240E1C2F1F0092797C /* hmac.h */,
464  
-				52A4251E0E1C2F170092797C /* sha1.c */,
  463
+				52A425230E1C2F1F0092797C /* hmac.c */,
465 464
 				52A4251F0E1C2F170092797C /* sha1.h */,
  465
+				52A4251E0E1C2F170092797C /* sha1.c */,
466 466
 				52857C990E1997B50011DA5C /* GHNSString+TimeInterval.h */,
467 467
 				52857C9A0E1997B50011DA5C /* GHNSString+TimeInterval.m */,
468 468
 				52857C9B0E1997B50011DA5C /* GHNSString+Utils.h */,
7  GHKitIPhone.xcodeproj/project.xcworkspace/contents.xcworkspacedata
... ...
@@ -0,0 +1,7 @@
  1
+<?xml version="1.0" encoding="UTF-8"?>
  2
+<Workspace
  3
+   version = "1.0">
  4
+   <FileRef
  5
+      location = "self:GHKitIPhone.xcodeproj">
  6
+   </FileRef>
  7
+</Workspace>
2  Scripts/iPhoneFramework.sh
@@ -70,3 +70,5 @@ cp Classes/*.h $FRAMEWORK_DIR/Headers/
70 70
 cp Classes/Swizzle/*.h $FRAMEWORK_DIR/Headers/
71 71
 cp Classes/iPhone/*.h $FRAMEWORK_DIR/Headers/
72 72
 cp FrameworkIOS.plist $FRAMEWORK_DIR/Resources/Info.plist
  73
+
  74
+echo "Framework: $FRAMEWORK_DIR"
11  Tests/NSDictionaryNSNullTest.m
@@ -37,18 +37,23 @@ - (void)testEmpty {
37 37
 	GHAssertEqualObjects(dict, expected, nil);	
38 38
 }
39 39
 
40  
-- (NSDictionary *)_testVAList:(id)obj keysAndObjects:(id)firstKey, ... {
  40
+- (NSDictionary *)_testVAList:(id)obj ignoreNil:(BOOL)ignoreNil keysAndObjects:(id)firstKey, ... {
41 41
 	va_list args;
42 42
   va_start(args, firstKey);
43  
-	NSDictionary *dict = [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args];
  43
+	NSDictionary *dict = [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args ignoreNil:ignoreNil];
44 44
 	va_end(args);
45 45
 	return dict;
46 46
 }
47 47
 
48 48
 - (void)testVAList {
49  
-	NSDictionary *dict = [self _testVAList:nil keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
  49
+	NSDictionary *dict = [self _testVAList:nil ignoreNil:NO keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
50 50
 	NSDictionary *expected = [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], @"key1", @"value2",  @"key2", [NSNull null], @"key3", nil];	
51 51
 	GHAssertEqualObjects(dict, expected, nil);
  52
+  
  53
+  NSDictionary *dict2 = [self _testVAList:nil ignoreNil:YES keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
  54
+	NSDictionary *expected2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value2",  @"key2", nil];	
  55
+	GHAssertEqualObjects(dict2, expected2, nil);
  56
+
52 57
 }
53 58
 
54 59
 @end
2  Tests/NSStringUtilsTest.m
@@ -134,9 +134,11 @@ - (void)testIsEqualIgnoreCase {
134 134
 	GHAssertTrue([@"FOoO" gh_isEqualIgnoreCase:@"fooO"], nil);
135 135
 }
136 136
 
  137
+#if !TARGET_OS_IPHONE
137 138
 - (void)testTruncateMiddle {
138 139
   NSAttributedString *attributedString = [@"This is a test" gh_truncateMiddle];
139 140
   GHTestLog(@"attributedString=%@", attributedString);
140 141
 }
  142
+#endif
141 143
 	
142 144
 @end

0 notes on commit e65768b

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