@mainpage FoundationExtension
此库包含一些Cocoa/UIKit扩展。此库不包含高级数据结构、算法或框架,而是代码片段的集合。
- 许多常用片段在方法调用中。
- 看起来像是原生基础方法 - 它遵循Apple编码指南和命名规范。
参阅[GitHub]上的文档(http://youknowone.github.com/FoundationExtension)
如何使用
- 编译后的库
- 构建项目
- 将FoundationExtension或UIKitExtension目标作为依赖项添加
- 直接源
- 将您需要的文件添加到您的项目中
- CocoaPod ~> 1.7.5
如果您的编译器是gcc或旧版本的clang,请将'-force_load'添加到静态库。
用于编辑的下载
git clone git://github.com/youknowone/FoundationExtension.git
cd FoundationExtension
git submodule update --init
为什么有用
让您的代码简短!不允许evil objc让您的代码冗长。此库包含许多常用工作的快捷方式。
从URL中获取NSData
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)
iPhone MAC地址
Foundation
- 无解。
FoundationExtension
[[UIDevice currentDevice] MACAddress]
@see @ref UIDevice(Shortcuts)
performSelector, with 3 objects
Foundation
- 无解。您应该使用 <objc/runtime.h>
FoundationExtension
[obj performSelector:sel withObject:o1 withObject:o2 withObject:o3];
@see @ref NSObject(ObjCRuntime)
从POST请求中获取NSData
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)
从多部分表单POST中获取NSData
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;
参见 @ref NSObject(ObjCRuntime) @see @ref NSObject(ObjCRuntimeClass)
从16进制字符串获取十六进制值
Foundation
int value;
sscanf(string.UTF8String, "%x", &value);
FoundationExtension
NSInteger value = [string hexadecimalValue];
为什么基础库有这个?
@see @ref NSData(Serialization)
Foundation
- 关于12进制字符串的说明?
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];
MD5哈希值
@see @ref NSData(CommonCrypto)
Foundation
- 从HTTP post请求解码plist字典
FoundationExtension
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPPostBody:@{@"key1":@"value1"} encoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithContentOfURLRequest:request];
NSDictionary *dictionary = [NSDictionary dictionaryWithData:data];
从NSURLRequest获取数据。呃.. 现在我能做什么呢?(使用NSPropertyListSerialization)
@see @ref NSMutableURLRequest(HTTPMethod) @see @ref NSData(NSURLRequest) @see @ref NSDictionary(NSData)
从HTML颜色代码创建UIColor
UIColor *color = [UIColor colorWithHTMLExpression:@"#f0f0f0"];
UIKitExtension
@see @ref UIColor(HTMLColor)
FoundationExtension
[class methodObjectForSelector:@selector(method1)].implementation
= [class methodObjectForSelector:@selector(method2)].implementation;
// now [obj method1] equals [obj method2]
将实现更改为新的一个
获取私有变量可访问性。
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)
更多信息
请查阅文档![http://youknowone.github.com/FoundationExtension] (http://youknowone.github.com/FoundationExtension)