@mainpage FoundationExtension
This library includes small Cocoa/UIKit extensions. This library does not includes high-level data structure, algorithm or frameworks, but collection of code snippets.
See document on [Github] (http://youknowone.github.com/FoundationExtension)
If your compiler is gcc or old clang, add '-force_load' to static library.
git clone git://github.com/youknowone/FoundationExtension.git
cd FoundationExtension
git submodule update --init
Make your code short! Do not allow evil objc to make your code verbose. This library includes many shortcuts for common work.
Foundation
NSString *URLString = [NSSring stringWithFormat:@"http://"HOST_URL"/api/%@", key];
NSURL *URL = [NSURL URLWithString:URLString];
FoundationExtension
NSURL *URL = [[@"http://"HOST_URL"/api/%@" format:key] URL];
@see @ref NSString(Shortcuts) @see @ref NSString(NSURL)
Foundation
FoundationExtension
[[UIDevice currentDevice] MACAddress]
@see @ref UIDevice(Shortcuts)
Foundation
FoundationExtension
[obj performSelector:sel withObject:o1 withObject:o2 withObject:o3];
@see @ref NSObject(ObjCRuntime)
Foundation
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:@"field1=value1"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
FoundationExtension
NSData *data = [NSData dataWithContentsOfURL:URL postBody:@{@"field1":@"value1"} encoding:NSUTF8StringEncoding];
@see @ref NSData(NSURLRequest)
Foundation
FoundationExtension
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMultiPartFormPostBody:@{@"filename":data} encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentOfURLRequest:request];
@see NSURLRequestAdditions.h
Foundation
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *s in array) {
[newArray addObject:[s substringToIndex:20]];
}
FoundationExtension NSArray
NSArray *newArray = [array arrayByMappingOperation:^(NSString *obj){ [obj substringToIndex:20]; }];
FoundationExtension NSMutableArray
[array map::^(NSString *obj){ [obj substringToIndex:20]; }];
@see @ref Map/Filter/Reduce @see NSAFunctional.h
Foundation
NSString *className = [NSString stringWithUTF8String:class_getName(obj.class)];
FoundationExtension
NSString *className = obj.class.name;
@see @ref NSObject(ObjCRuntime) @see @ref NSObject(ObjCRuntimeClass)
Foundation
int value;
sscanf(string.UTF8String, "%x", &value);
FoundationExtension
NSInteger value = [string hexadecimalValue];
@see @ref NSData(Serialization)
Foundation
FoundationExtension
NSInteger value = [string integerValueBase:12];
@see @ref NSString(Evaluation)
Foundation
unsigned char hashedChars[CC_MD5_DIGEST_LENGTH];
CC_MD5([data bytes], (CC_LONG)[self length], hashedChars);
NSMutableString *result = [[NSMutableString alloc] init];
for ( int i = 0; i<CC_MD5_DIGEST_LENGTH; i++ ) {
[result appendFormat:@"%02x", *(hashedChars+i)];
}
FoundationExtension
NSString *result = [data digestStringByMD5];
@see @ref NSData(CommonCrypto)
Foundation
FoundationExtension
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPPostBody:@{@"key1":@"value1"} encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentOfURLRequest:request];
NSDictionary *dictionary = [NSDictionary dictionaryWithData:data];
@see @ref NSMutableURLRequest(HTTPMethod) @see @ref NSData(NSURLRequest) @see @ref NSDictionary(NSData)
UIKitExtension
UIColor *color = [UIColor colorWithHTMLExpression:@"#f0f0f0"];
@see @ref UIColor(HTMLColor)
FoundationExtension
[class methodObjectForSelector:@selector(method1)].implementation
= [class methodObjectForSelector:@selector(method2)].implementation;
// now [obj method1] equals [obj method2]
@see NSAMethod
FoundationExtension
@interface Secret: NSObject { @private id _attr; } @end // #1 remember the '_attr'
// Hack the Secret!
@interface Secret (Accessor)
@property
(nonatomic, retain) id attr; // #2 remember the 'attr'
@end
@implementation Secret (Accessor)
NSAPropertyGetter(attr, "_attr") // #2, #1 to create getter
NSAPropertyRetainSetter(setAttr, "_attr") // #2, #1 to create getter
@end
@see NSObject(ObjCRuntime)
See the document! [http://youknowone.github.com/FoundationExtension] (http://youknowone.github.com/FoundationExtension)