Permalink
Browse files

Adding ignoreNil option

  • Loading branch information...
1 parent 1229348 commit e65768b035443d0d8e531e85d0a48944d9862759 @gabriel committed Jun 5, 2011
View
@@ -5,3 +5,4 @@ build
xcuserdata
Documentation/*
GHKit.docset
+GHKitIPhone.xcodeproj/project.xcworkspace/xcuserdata
@@ -38,10 +38,42 @@
[NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNil:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
@param firstObject... Alternating key, value pairs. Terminated when _key_ is nil.
+ @result Dictionary
*/
+ (id)gh_dictionaryWithKeysAndObjectsMaybeNil:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;
-+ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args;
+/*!
+ Create dictionary which supports nil values.
+ Key is first (instead of value then key). If the value is nil it is stored internally as NSNull,
+ and when calling objectMaybeNilForKey will return nil.
+
+ For example,
+ @code
+ - (void)setDictionary:(id)key, ... {
+ va_list args;
+ va_start(args, key);
+ [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:key args:args ignoreNilValues:NO];
+ va_end(args);
+ }
+ @endcode
+
+ @param firstKey First key
+ @param args Args va_list via va_start
+ @param ignoreNil If YES, will not store key, value paries where the value is nil
+ @result Dictionary
+ */
++ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args ignoreNil:(BOOL)ignoreNil;
+
+/*!
+ Create dictionary which supports nil values.
+ Key is first (instead of value then key). If the value is nil it is stored internally as NSNull,
+ and when calling objectMaybeNilForKey will return nil.
+
+ For example,
+ [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNil:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
+
+ @param firstObject... Alternating key, value pairs. Terminated when _key_ is nil.
+ */
/*!
Use this method instead of objectForKey if you want nil (and not the internal NSNull).
@@ -28,18 +28,22 @@
@implementation NSDictionary(GHNSNull)
-+ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args {
++ (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)args ignoreNil:(BOOL)ignoreNil {
if (!firstKey) return [self dictionary];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableArray *values = [[NSMutableArray alloc] init];
id key = firstKey;
do {
id value = va_arg(args, id);
+ if (!value && ignoreNil) {
+ key = va_arg(args, id);
+ continue;
+ }
if (!value) value = [NSNull null];
[keys addObject:key];
[values addObject:value];
- key = va_arg(args,id);
+ key = va_arg(args, id);
} while(key);
NSDictionary *dict = [self dictionaryWithObjects:values forKeys:keys];
[keys release];
@@ -50,7 +54,7 @@ + (id)gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:(id)firstKey args:(va_list)
+ (id)gh_dictionaryWithKeysAndObjectsMaybeNil:(id)firstKey, ... {
va_list args;
va_start(args, firstKey);
- NSDictionary *dict = [self gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args];
+ NSDictionary *dict = [self gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args ignoreNil:NO];
va_end(args);
return dict;
}
@@ -459,10 +459,10 @@
52857C920E1997B50011DA5C /* GHNSFileManager+Utils.m */,
52857C950E1997B50011DA5C /* GHNSString+HMAC.h */,
52857C960E1997B50011DA5C /* GHNSString+HMAC.m */,
- 52A425230E1C2F1F0092797C /* hmac.c */,
52A425240E1C2F1F0092797C /* hmac.h */,
- 52A4251E0E1C2F170092797C /* sha1.c */,
+ 52A425230E1C2F1F0092797C /* hmac.c */,
52A4251F0E1C2F170092797C /* sha1.h */,
+ 52A4251E0E1C2F170092797C /* sha1.c */,
52857C990E1997B50011DA5C /* GHNSString+TimeInterval.h */,
52857C9A0E1997B50011DA5C /* GHNSString+TimeInterval.m */,
52857C9B0E1997B50011DA5C /* GHNSString+Utils.h */,
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Workspace
+ version = "1.0">
+ <FileRef
+ location = "self:GHKitIPhone.xcodeproj">
+ </FileRef>
+</Workspace>
@@ -70,3 +70,5 @@ cp Classes/*.h $FRAMEWORK_DIR/Headers/
cp Classes/Swizzle/*.h $FRAMEWORK_DIR/Headers/
cp Classes/iPhone/*.h $FRAMEWORK_DIR/Headers/
cp FrameworkIOS.plist $FRAMEWORK_DIR/Resources/Info.plist
+
+echo "Framework: $FRAMEWORK_DIR"
@@ -37,18 +37,23 @@ - (void)testEmpty {
GHAssertEqualObjects(dict, expected, nil);
}
-- (NSDictionary *)_testVAList:(id)obj keysAndObjects:(id)firstKey, ... {
+- (NSDictionary *)_testVAList:(id)obj ignoreNil:(BOOL)ignoreNil keysAndObjects:(id)firstKey, ... {
va_list args;
va_start(args, firstKey);
- NSDictionary *dict = [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args];
+ NSDictionary *dict = [NSDictionary gh_dictionaryWithKeysAndObjectsMaybeNilWithKey:firstKey args:args ignoreNil:ignoreNil];
va_end(args);
return dict;
}
- (void)testVAList {
- NSDictionary *dict = [self _testVAList:nil keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
+ NSDictionary *dict = [self _testVAList:nil ignoreNil:NO keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
NSDictionary *expected = [NSDictionary dictionaryWithObjectsAndKeys:[NSNull null], @"key1", @"value2", @"key2", [NSNull null], @"key3", nil];
GHAssertEqualObjects(dict, expected, nil);
+
+ NSDictionary *dict2 = [self _testVAList:nil ignoreNil:YES keysAndObjects:@"key1", nil, @"key2", @"value2", @"key3", nil, nil];
+ NSDictionary *expected2 = [NSDictionary dictionaryWithObjectsAndKeys:@"value2", @"key2", nil];
+ GHAssertEqualObjects(dict2, expected2, nil);
+
}
@end
@@ -134,9 +134,11 @@ - (void)testIsEqualIgnoreCase {
GHAssertTrue([@"FOoO" gh_isEqualIgnoreCase:@"fooO"], nil);
}
+#if !TARGET_OS_IPHONE
- (void)testTruncateMiddle {
NSAttributedString *attributedString = [@"This is a test" gh_truncateMiddle];
GHTestLog(@"attributedString=%@", attributedString);
}
+#endif
@end

0 comments on commit e65768b

Please sign in to comment.