Overline 0.2.0
August 16, 2013 · View on GitHub
Objective-C utilities and shorthands.
Overline is not clone of other language features. Simple and clean Objective-C.
Usage
setup with cocoapods.
pod 'Overline'
import Overline.h
#import "Overline.h"
with BlocksKit
pod 'BlocksKit'
pod 'Overline-BlocksKit'
Ignored duplicate methods: each map reduce filter reject
Features
NSArray
NSArray+Initialize
NSArray *array = [NSArray arrayWithRangeFrom:0 to:10];
// @[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9]
NSArray *array2 = [NSArray arrayWithRangeFrom:0 to:-10 step:2];
// @[@0, @-2, @-4, @-6, @-8]
arrayWithRangeFrom:to:arrayWithRangeFrom:to:step:
NSArray+Enumeration
NSArray *mapped = [@[@1,@2,@3,@4,@5,@6] mappedArrayUsingBlock:^id(id obj, NSUInteger idx) {
if ([obj integerValue] % 2 == 0) {
return obj;
}
return nil;
}];
// @[@2,@4,@6]
eachmapmappedArrayUsingBlockreducereducedObjectByBlockfindobjectUsingBlockfilterfilteredArrayUsingBlockrejectrejectedArrayUsingBlock
NSArray+Random
NSArray *shuffled = [@[@1,@2,@3,@4,@5,@6] shuffledArray];
shuffleshuffledArrayanyObject
NSArray+Reverse
NSArray *reversed = [@[@1,@2,@3,@4,@5,@6] reversedArray];
// @[@6,@5,@4,@3,@2,@1]
reversereversedArrayobjectAtReversedIndex
NSArray+Selector
firstObject
NSArray+SubArray
subarrayFromIndex:subarrayFromIndex:length:subarrayToIndex:uniqueObjectsuniqueObjectsUsingEqualsBlock
NSArray+Difference
differencearrayDifferenceWithArrayunionisearrayByUnionisingArray
NSArray+Shorthand
isEmpty
NSArray+Sorting
arraySortedDescending:arraySortedAscarraySortedDesc
NSMutableArray
NSMutableArray+Shorthands
NSMutableArray *marray = [NSMutableArray arrayWithArray:@[@1,@2,@3]];
[marray insertObjects:@[@4,@5,@6] atIndex:1];
insertObjects:atIndex:
NSMutableArray+Sorting
sortDescending:sortAscsortDesc
NSDate
NSDate+Components
Class Methods
currentWeekdaycurrentHourcurrentMinutecurrentSecondcurrentDaycurrentMonthcurrentYearcommonDateComponentsForNowtimeComponentsForNow
Instance Methods
weekdayhourminuteseconddaymonthyearcommonDateComponentstimeComponents
NSDictionary
NSDictionary+Selector
NSDate *date = [JSON objectForKey:@"date8601" transformBlock:^id(id obj) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss Z"];
return [formatter dateFromString:obj];
}];
objectForKey:transformBlock:numberForKey:stringForKey:arrayForKey:dictionaryForKey:dateForKey:dateForKey:usingFormat:since1970DateForKey:timestampForKey:boolObjectForKey:withTrueValue:boolForKey:withTureValue:boolForKey:intForKey:integerForKey:unsignedIntForKey:unsignedIntegerForKey:longForKey:unsignedLongForKey:longLongForKey:unsignedLongLongForKey:doubleForKey:floatForKey:
NSDictionary+Enumeration
NSDictionary *dic = @{
@"a" : @1,
@"b" : @2,
@"c" : @3,
@"d" : @4
};
NSDictionary *filtered = [dic dictionaryFilteredByKeyUsingBlock:^BOOL(NSString *key) {
return [key isEqualToString:@"a"];
}]; // @{@"a":@1}
each:mapmappedDictionaryUsingBlockarrayMapmappedArrayUsingBlockfilterKeydictionaryFilteredByKeyUsingBlockrejectKeydictionaryRejectedByKeyUsingBlockfilterObjectdictionaryFilteredByObjectUsingBlockrejectObjectdictionaryRejectedByObjectUsingBlockmergedictionaryByMergingDictionaryreduce:reducedObjectUsingBlock:memo:queryStringstringByFormattingQuery
NSDictionary+Path
/*
@{
@"error" : @{
@"message" : @"msg"
}
}
*/
NSString *s = [dic objectForPath:@"error.message"];
objectForPath:
NSSet
NSSet+Enumeration
NSSet *set = [NSSet setWithArray:@[@1,@2,@3,@4,@5,@6]];
NSSet *mapped = [set mappedSetUsingBlock:^id(id obj, NSUInteger idx) {
if ([obj integerValue] % 2 == 0) {
return obj;
}
return nil;
}];
eachmapmappedSetUsingBlockreducereducedObjectByBlockfilterfilteredSetUsingBlockrejectrejectedSetUsingBlock
NSString
NSString+Components
[@"hoge=fuga&piyo=foo" componentsSeparatedByInnerString:@"=" andOuterString:@"&"];
/*
@{
@"hoge" : @"fuga",
@"piyo" : @"foo"
};
*/
componentsSeparatedByInnerString:andOuterString:
NSString+RegularExpression
[@"https?" testInString:urlString];
rangeOfFirstMatchInString:rangeOfFirstMatchInString:options:matchesInString:matchesInString:options:testInString:testInString:options:replace:newString:stringByReplacingOccurrencesOfRegExpPattern:withStringreplace:newString:options:stringByReplacingOccurrencesOfRegExpPattern:withString:options:replace:template:stringByReplacingOccurrencesOfRegExpPattern:withTemplate:replace:template:options:stringByReplacingOccurrencesOfRegExpPattern:withTemplate:options:
NSString+Hash
[@"hoge" md5]; // @"ea703e7aa1efda0064eaa507d9e8ab7e"
sha256stringByHashingSha256md5stringByHashingMD5
NSString+URLEncode
URLEncodestringByEncodingURLURLDecodestringByDecodingURL
NSString+Shorthand
trimtrim:- Trim character set in a given string
NSString+JSON
NSString *JSONString = @"{\"hoge\" : \"fuga\" }";
id json = [JSONString jsonObject];
JSONjsonObject
NSString+Base64Encode
[@"a" stringByEncodingBase64]; // @"YQ=="
[@"YQ==" stringByDecodingBase64]; // @"a"
encodeBase64stringByEncodingBase64decodeBase64stringByDecodingBase64dataUsingDecodingBase64
NSData
NSData+Base64Encode
base64EncodestringUsingEncodingBase64
NSURL
NSURL+Components
queryComponents
NSURL+Directories
NSURL *pdfURL = [NSURL URLForUserDirectoryWithAppendedPath:@"myDoc.pdf"];
NSURL *userImageFolderURL = [NSURL URLForUserDirectoryWithAppendedPath:@"images"];
URLForDirectory:domainMask:URLForApplicationSupportDataDirectoryURLForApplicationSupportWithAppendedPath:URLForUserDirectoryURLForUserDirectoryWithAppendedPath:URLForDocumentDirectory;URLForDocumentDirectoryWithAppendedPath:;
NSBundle
NSBundle+Shorthand
JSONForResourceName
NSNumber
NSNumber+Equals
[@1 isEqualToInteger:1];
isEqualToInt:isEqualToInteger:isEqualToUnsignedInt:isEqualToUnsignedInteger:isEqualToLong:isEqualToLongLong:isEqualToUnsignedLong:isEqualToUnsignedLongLong:isEqualToDouble:isEqualToFloat:
NSDateFormatter
NSDateFormatter+Shothand
userDefaultFormatterdateFormatterWithCalendarIndetifiter:localeIdentifiter:timeZoneAbbreviation:
NSObject
NSObject+Equals
[[NSNull null] isNullObject]; // YES
[[NSArray array] isNullObject]; // NO
isNullObjectisArrayisDictionaryisSetisStringisNumber
NSNull
NSNull+Natural
Provide NSNull like nil
NSDictionary *dic = @{
@"null-key" : [NSNull null]
};
[[dic objectForKey:@"null-key"] objectForKey:@"empty"]; // nil
※ This feature disable on default. Define OV_USE_NATURAL_NULL, if use this feature.
#define OV_USE_NATURAL_NULL
#import "Overline.h"
Credits
Be sure to thank these contributors:
- Kazuma Ukyo (Creator, http://yaakaito.org)
- Hari Karam Singh (Contributor, http://soundwandapp.com)
Coding rules
- Write tests.
- Objective-C style naming.
- and shorthands.