Skip to content

Instantly share code, notes, and snippets.

@Yulong
Created May 14, 2014 15:09
Show Gist options
  • Save Yulong/229a62c1188c3c024247 to your computer and use it in GitHub Desktop.
Save Yulong/229a62c1188c3c024247 to your computer and use it in GitHub Desktop.
extract NSDictionary(BeeExtension) from BeeFramework.
//
// ______ ______ ______
// /\ __ \ /\ ___\ /\ ___\
// \ \ __< \ \ __\_ \ \ __\_
// \ \_____\ \ \_____\ \ \_____\
// \/_____/ \/_____/ \/_____/
//
//
// Copyright (c) 2014-2015, Geek Zoo Studio
// http://www.bee-framework.com
//
//
// 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.
//
#pragma mark -
@interface NSDictionary(BeeExtension)
- (id)objectOfAny:(NSArray *)array;
- (NSString *)stringOfAny:(NSArray *)array;
- (id)objectAtPath:(NSString *)path;
- (id)objectAtPath:(NSString *)path otherwise:(NSObject *)other;
- (id)objectAtPath:(NSString *)path separator:(NSString *)separator;
- (id)objectAtPath:(NSString *)path otherwise:(NSObject *)other separator:(NSString *)separator;
- (BOOL)boolAtPath:(NSString *)path;
- (BOOL)boolAtPath:(NSString *)path otherwise:(BOOL)other;
- (NSNumber *)numberAtPath:(NSString *)path;
- (NSNumber *)numberAtPath:(NSString *)path otherwise:(NSNumber *)other;
- (NSString *)stringAtPath:(NSString *)path;
- (NSString *)stringAtPath:(NSString *)path otherwise:(NSString *)other;
- (NSArray *)arrayAtPath:(NSString *)path;
- (NSArray *)arrayAtPath:(NSString *)path otherwise:(NSArray *)other;
- (NSMutableArray *)mutableArrayAtPath:(NSString *)path;
- (NSMutableArray *)mutableArrayAtPath:(NSString *)path otherwise:(NSMutableArray *)other;
- (NSDictionary *)dictAtPath:(NSString *)path;
- (NSDictionary *)dictAtPath:(NSString *)path otherwise:(NSDictionary *)other;
- (NSMutableDictionary *)mutableDictAtPath:(NSString *)path;
- (NSMutableDictionary *)mutableDictAtPath:(NSString *)path otherwise:(NSMutableDictionary *)other;
@end
#pragma mark -
@interface NSMutableDictionary(BeeExtension)
- (NSString *)stringOfAny:(NSArray *)array removeAll:(BOOL)flag;
- (BOOL)setObject:(NSObject *)obj atPath:(NSString *)path;
- (BOOL)setObject:(NSObject *)obj atPath:(NSString *)path separator:(NSString *)separator;
- (BOOL)setKeyValues:(id)first, ...;
+ (NSMutableDictionary *)keyValues:(id)first, ...;
@end
//
// ______ ______ ______
// /\ __ \ /\ ___\ /\ ___\
// \ \ __< \ \ __\_ \ \ __\_
// \ \_____\ \ \_____\ \ \_____\
// \/_____/ \/_____/ \/_____/
//
//
// Copyright (c) 2014-2015, Geek Zoo Studio
// http://www.bee-framework.com
//
//
// 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.
//
#import "NSDictionary+BeeExtension.h"
// ----------------------------------
// Source code
// ----------------------------------
#pragma mark -
@implementation NSDictionary(BeeExtension)
- (NSObject *)objectOfAny:(NSArray *)array
{
for ( NSString * key in array )
{
NSObject * obj = [self objectForKey:key];
if ( obj )
return obj;
}
return nil;
}
- (NSString *)stringOfAny:(NSArray *)array
{
NSObject * obj = [self objectOfAny:array];
if ( nil == obj )
return nil;
return [obj isKindOfClass:[NSString class]]?(NSString*)obj:[obj description];
}
- (id)objectAtPath:(NSString *)path
{
return [self objectAtPath:path separator:nil];
}
- (id)objectAtPath:(NSString *)path separator:(NSString *)separator
{
if ( nil == separator )
{
path = [path stringByReplacingOccurrencesOfString:@"." withString:@"/"];
separator = @"/";
}
NSArray * array = [path componentsSeparatedByString:separator];
if ( 0 == [array count] )
{
return nil;
}
NSObject * result = nil;
NSDictionary * dict = self;
for ( NSString * subPath in array )
{
if ( 0 == [subPath length] )
continue;
result = [dict objectForKey:subPath];
if ( nil == result )
return nil;
if ( [array lastObject] == subPath )
{
return result;
}
else if (![result isKindOfClass:[NSDictionary class]])
{
return nil;
}
dict = (NSDictionary *)result;
}
return (result == [NSNull null]) ? nil : result;
}
- (id)objectAtPath:(NSString *)path otherwise:(NSObject *)other
{
NSObject * obj = [self objectAtPath:path];
return obj ? obj : other;
}
- (id)objectAtPath:(NSString *)path otherwise:(NSObject *)other separator:(NSString *)separator
{
NSObject * obj = [self objectAtPath:path separator:separator];
return obj ? obj : other;
}
- (BOOL)boolAtPath:(NSString *)path
{
return [self boolAtPath:path otherwise:NO];
}
- (BOOL)boolAtPath:(NSString *)path otherwise:(BOOL)other
{
NSObject * obj = [self objectAtPath:path];
if ( [obj isKindOfClass:[NSNull class]] )
{
return NO;
}
else if ( [obj isKindOfClass:[NSNumber class]] )
{
return [(NSNumber *)obj intValue] ? YES : NO;
}
else if ( [obj isKindOfClass:[NSString class]] )
{
if ( [(NSString *)obj hasPrefix:@"y"] ||
[(NSString *)obj hasPrefix:@"Y"] ||
[(NSString *)obj hasPrefix:@"T"] ||
[(NSString *)obj hasPrefix:@"t"] ||
[(NSString *)obj isEqualToString:@"1"] )
{
// YES/Yes/yes/TRUE/Ture/true/1
return YES;
}
else
{
return NO;
}
}
return other;
}
- (NSNumber *)numberAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
if ( [obj isKindOfClass:[NSNull class]] )
{
return nil;
}
else if ( [obj isKindOfClass:[NSNumber class]] )
{
return (NSNumber *)obj;
}
else if ( [obj isKindOfClass:[NSString class]] )
{
return [NSNumber numberWithDouble:[(NSString *)obj doubleValue]];
}
return nil;
}
- (NSNumber *)numberAtPath:(NSString *)path otherwise:(NSNumber *)other
{
NSNumber * obj = [self numberAtPath:path];
return obj ? obj : other;
}
- (NSString *)stringAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
if ( [obj isKindOfClass:[NSNull class]] )
{
return nil;
}
else if ( [obj isKindOfClass:[NSNumber class]] )
{
return [NSString stringWithFormat:@"%d", [(NSNumber *)obj intValue]];
}
else if ( [obj isKindOfClass:[NSString class]] )
{
return (NSString *)obj;
}
return nil;
}
- (NSString *)stringAtPath:(NSString *)path otherwise:(NSString *)other
{
NSString * obj = [self stringAtPath:path];
return obj ? obj : other;
}
- (NSArray *)arrayAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
return [obj isKindOfClass:[NSArray class]] ? (NSArray *)obj : nil;
}
- (NSArray *)arrayAtPath:(NSString *)path otherwise:(NSArray *)other
{
NSArray * obj = [self arrayAtPath:path];
return obj ? obj : other;
}
- (NSMutableArray *)mutableArrayAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
return [obj isKindOfClass:[NSMutableArray class]] ? (NSMutableArray *)obj : nil;
}
- (NSMutableArray *)mutableArrayAtPath:(NSString *)path otherwise:(NSMutableArray *)other
{
NSMutableArray * obj = [self mutableArrayAtPath:path];
return obj ? obj : other;
}
- (NSDictionary *)dictAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
return [obj isKindOfClass:[NSDictionary class]] ? (NSDictionary *)obj : nil;
}
- (NSDictionary *)dictAtPath:(NSString *)path otherwise:(NSDictionary *)other
{
NSDictionary * obj = [self dictAtPath:path];
return obj ? obj : other;
}
- (NSMutableDictionary *)mutableDictAtPath:(NSString *)path
{
NSObject * obj = [self objectAtPath:path];
return [obj isKindOfClass:[NSMutableDictionary class]] ? (NSMutableDictionary *)obj : nil;
}
- (NSMutableDictionary *)mutableDictAtPath:(NSString *)path otherwise:(NSMutableDictionary *)other
{
NSMutableDictionary * obj = [self mutableDictAtPath:path];
return obj ? obj : other;
}
@end
#pragma mark -
@implementation NSMutableDictionary(BeeExtension)
- (NSString *)stringOfAny:(NSArray *)array removeAll:(BOOL)flag
{
NSString * result = [self stringOfAny:array];
if ( flag )
{
[self removeObjectsForKeys:array];
}
return result;
}
- (BOOL)setObject:(NSObject *)obj atPath:(NSString *)path
{
return [self setObject:obj atPath:path separator:nil];
}
- (BOOL)setObject:(NSObject *)obj atPath:(NSString *)path separator:(NSString *)separator
{
if ( 0 == [path length] )
return NO;
if ( nil == separator )
{
path = [path stringByReplacingOccurrencesOfString:@"." withString:@"/"];
separator = @"/";
}
NSArray * array = [path componentsSeparatedByString:separator];
if ( 0 == [array count] )
{
[self setObject:obj forKey:path];
return YES;
}
NSMutableDictionary * upperDict = self;
NSDictionary * dict = nil;
NSString * subPath = nil;
for ( subPath in array )
{
if ( 0 == [subPath length] )
continue;
if ( [array lastObject] == subPath )
break;
dict = [upperDict objectForKey:subPath];
if ( nil == dict )
{
dict = [NSMutableDictionary dictionary];
[upperDict setObject:dict forKey:subPath];
}
else
{
if (![dict isKindOfClass:[NSDictionary class]])
return NO;
if (![dict isKindOfClass:[NSMutableDictionary class]])
{
dict = [NSMutableDictionary dictionaryWithDictionary:dict];
[upperDict setObject:dict forKey:subPath];
}
}
upperDict = (NSMutableDictionary *)dict;
}
[upperDict setObject:obj forKey:subPath];
return YES;
}
- (BOOL)setKeyValues:(id)first, ...
{
va_list args;
va_start( args, first );
for ( ;; first = nil )
{
NSObject * key = first ? first : va_arg( args, NSObject * );
if ( nil == key || ![key isKindOfClass:[NSString class]])
break;
NSObject * value = va_arg( args, NSObject * );
if ( nil == value )
break;
BOOL ret = [self setObject:value atPath:(NSString *)key];
if (!ret) {
va_end( args );
return NO;
}
}
va_end( args );
return YES;
}
+ (NSMutableDictionary *)keyValues:(id)first, ...
{
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
va_list args;
va_start( args, first );
for ( ;; first = nil )
{
NSObject * key = first ? first : va_arg( args, NSObject * );
if ( nil == key || ![key isKindOfClass:[NSString class]])
break;
NSObject * value = va_arg( args, NSObject * );
if ( nil == value )
break;
[dict setObject:value atPath:(NSString *)key];
}
va_end( args );
return dict;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment