用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
来源:互联网 发布:web软件开发费用 编辑:程序博客网 时间:2024/06/08 16:36
source - https://github.com/supermarin/ObjectiveSugar
Look like a girl, act like a lady, think like a man, work like a boss.
外表如萝莉,举止赛淑女,思想堪汉子,工作比老板。
A set of functional additions for Foundation you wish you've had at the first place.
这是一个关于Foundation框架的一系列的扩展,让你魂牵梦断的东东。
Usage
Install via CocoaPods
pod 'ObjectiveSugar'
Import the public header
#import <ObjectiveSugar/ObjectiveSugar.h>
Documentation
NSNumber additions
NSNumber 扩展
重复3次[@3 times:^{ NSLog(@"Hello!");}];// Hello!// Hello!// Hello!重复3次,并附带标签[@3 timesWithIndex:^(NSUInteger index) { NSLog(@"Another version with number: %d", index);}];// Another version with number: 0// Another version with number: 1// Another version with number: 2从1到4[@1 upto:4 do:^(NSInteger numbah) { NSLog(@"Current number.. %d", numbah);}];// Current number.. 1// Current number.. 2// Current number.. 3// Current number.. 4从7到4[@7 downto:4 do:^(NSInteger numbah) { NSLog(@"Current number.. %d", numbah);}];// Current number.. 7// Current number.. 6// Current number.. 5// Current number.. 4NSDate *firstOfDecember = [NSDate date]; // let's pretend it's 1st of December从firstOfDecember之后的第30天NSDate *firstOfNovember = [@30.days since:firstOfDecember];// 2012-11-01 00:00:00 +0000newYearsDay之前的第7天NSDate *christmas = [@7.days until:newYearsDay];// 2012-12-25 00:00:00 +0000从现在之后的第24天NSDate *future = @24.days.fromNow;// 2012-12-25 20:49:05 +0000一个月之前NSDate *past = @1.month.ago;// 2012-11-01 20:50:28 +00:00
NSArray / NSSet additions
NSArray / NSSet 扩展
// All of these methods return a modified copy of the array.// They're not modifying the source array.所有的这些方法返回了一个修改过的array的copy备份他们没有修改原始的arrayNSArray *cars = @[@"Testarossa", @"F50", @"F458 Italia"]; // or NSSet取数组中每一个元素[cars each:^(id object) { NSLog(@"Car: %@", object);}];// Car: Testarossa// Car: F50// Car: F458 Italia取数组中每一个元素,并附带标签[cars eachWithIndex:^(id object, NSUInteger index) { NSLog(@"Car: %@ index: %i", object, index);}];// Car: Testarossa index: 0// Car: F50 index: 1// Car: F458 Italia index: 2倒序输出数组[cars each:^(id object) { NSLog(@"Car: %@", object);} options:NSEnumerationReverse];// Car: F458 Italia// Car: F50// Car: Testarossa倒序输出数组并附带标签[cars eachWithIndex:^(id object, NSUInteger index) { NSLog(@"Car: %@ index: %i", object, index);} options:NSEnumerationReverse];// Car: F458 Italia index: 2// Car: F50 index: 1// Car: Testarossa index: 0????????[cars map:^(NSString* car) { return car.lowercaseString;}];// testarossa, f50, f458 italia????????// Or, a more common example:[cars map:^(NSString* carName) { return [[Car alloc] initWithName:carName];}];// array of Car objectsNSArray *mixedData = @[ @1, @"Objective Sugar!", @"Github", @4, @"5"];过滤出指定类型的对象[mixedData select:^BOOL(id object) { return ([object class] == [NSString class]);}];// Objective Sugar, Github, 5屏蔽掉指定类型的对象[mixedData reject:^BOOL(id object) { return ([object class] == [NSString class]);}];// 1, 4排序NSArray *numbers = @[ @5, @2, @7, @1 ];[numbers sort];// 1, 2, 5, 7cars.sample// 458 Italiacars.sample// F50
NSArray only
NSArray 单独的
NSArray *numbers = @[@1, @2, @3, @4, @5, @6];从2到4// index from 2 to 4numbers[@"2..4"];// [@3, @4, @5]从2到3// index from 2 to 4 (excluded)numbers[@"2...4"];// [@3, @4]从2开始,之后有4个// With NSRange location: 2, length: 4numbers[@"2,4"];// [@3, @4, @5, @6]从2开始,之后有4个NSValue *range = [NSValue valueWithRange:NSMakeRange(2, 4)];numbers[range];// [@3, @4, @5, @6]数组反转[numbers reverse];// [@6, @5, @4, @3, @2, @1]NSArray *fruits = @[ @"banana", @"mango", @"apple", @"pear" ];数组中包含apple字符串[fruits includes:@"apple"];// YES从数组中取3个[fruits take:3];// banana, mango, apple取数组中元素,知道出现apple后停止[fruits takeWhile:^BOOL(id fruit) { return ![fruit isEqualToString:@"apple"];}];// banana, mango将数组套数组扁平化NSArray *nestedArray = @[ @[ @1, @2, @3 ], @[ @4, @5, @6, @[ @7, @8 ] ], @9, @10 ];[nestedArray flatten];// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10拼接数组字符串NSArray *abc = @[ @"a", @"b", @"c" ];[abc join];// abc按照指定的方式拼接字符串[abc join:@"-"];// a-b-cNSArray *mixedData = @[ @1, @"Objective Sugar!", @"Github", @4, @"5"];检测指定类型的数据,发现后返回[mixedData detect:^BOOL(id object) { return ([object class] == [NSString class]);}];// Objective Sugar// TODO: Make a better / simpler example of this这些都是没写完的功能,后续版本添加上-_-!NSArray *landlockedCountries = @[ @"Bolivia", @"Paraguay", @"Austria", @"Switzerland", @"Hungary" ];NSArray *europeanCountries = @[ @"France", @"Germany", @"Austria", @"Spain", @"Hungary", @"Poland", @"Switzerland" ];[landlockedCountries intersectionWithArray:europeanCountries];// landlockedEuropeanCountries = Austria, Switzerland, Hungary[landlockedCountries unionWithArray:europeanCountries];// landlockedOrEuropean = Bolivia, Paraguay, Austria, Switzerland, Hungary, France, Germany, Spain, Poland[landlockedCountries relativeComplement:europeanCountries];// nonEuropeanLandlockedCountries = Bolivia, Paraguay[europeanCountries relativeComplement:landlockedCountries];// notLandlockedEuropeanCountries = France, Germany, Spain, Poland[landlockedCountries symmetricDifference:europeanCountries];// uniqueCountries = Bolivia, Paraguay, France, Germany, Spain, Poland
NSMutableArray additions
NSMutableArray 扩展
NSMutableArray *people = @[ @"Alice", @"Benjamin", @"Christopher" ];插入一个对象[people push:@"Daniel"]; // Alice, Benjamin, Christopher, Daniel移除一个对象[people pop]; // Daniel// people = Alice, Benjamin, Christopher移除第二个位置的对象[people pop:2]; // Benjamin, Christopher// people = Alice插入多个对象[people concat:@[ @"Evan", @"Frank", @"Gavin" ]];// people = Alice, Evan, Frank, Gavin
NSDictionary additions
NSDictionary 扩展
NSDictionary *dict = @{ @"one" : @1, @"two" : @2, @"three" : @3 };字典中每一个对象[dict each:^(id key, id value){ NSLog(@"Key: %@, Value: %@", key, value);}];// Key: one, Value: 1// Key: two, Value: 2// Key: three, Value: 3字典中每一个key[dict eachKey:^(id key) { NSLog(@"Key: %@", key);}];// Key: one// Key: two// Key: three字典中每一个value[dict eachValue:^(id value) { NSLog(@"Value: %@", value);}];// Value: 1// Value: 2// Value: 3NSDictionary *errors = @{ @"username" : @[ @"already taken" ], @"password" : @[ @"is too short (minimum is 8 characters)", @"not complex enough" ], @"email" : @[ @"can't be blank" ];};将key与value合并在一起[errors map:^(id attribute, id reasons) { return NSStringWithFormat(@"%@ %@", attribute, [reasons join:@", "]);}];// username already taken// password is too short (minimum is 8 characters), not complex enough// email can't be blank检测是否含有哪个key[errors hasKey:@"email"]// true[errors hasKey:@"Alcatraz"]// false
NSString additions
NSString 扩展
NSString *sentence = NSStringWithFormat(@"This is a text-with-argument %@", @1234);// This is a text-with-argument 1234按照空格分隔字符串[sentence split];// array = this, is, a, text-with-argument, 1234按照指定字符串分隔[sentence split:@"-"]// array = this is a text, with, argument 1234检测是否含有某个字符串[sentence containsString:@"this is a"];// YES
C additions
C 扩展
_messages为false时执行unless(_messages) { // The body is only executed if the condition is false _messages = [self initializeMessages];}直到iterations为0时停止int iterations = 10;until(iterations == 0) { // The body is executed until the condition is false // 10 9 8 7 6 5 4 3 2 1 printf("%d ", iterations); iterations--;}printf("\n");至少会执行一次,而直到这个条件为false时iterations = 10;do { // The body is executed at least once until the condition is false // Will print: Executed! printf("Executed!\n");} until(true);
0 0
- 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
- 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
- Objective C - Foundation - NSNumber & NSString & NSArray & NSDictionary & NSSet
- 容器类的常用操作(NSArray,NSNumber,NSDictionary,NSSet)
- Foundation框架—NSArray、NSSet、NSDictionary、NSNumber、NSDate、NSValue
- OC第五讲(NSNumber,NSArray,NSDictionary,NSSet等)
- NSArray,NSSet,NSDictionary
- NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSDictionary总结
- NSArray NSSet NSDictionary
- NSDictionary,NSArray,NSSet
- NSArray,NSSet,NSDictionary总结
- NSArray,NSSet,NSDictionary
- NSArray NSSet NSDictionary
- NSArray、 NSSet、 NSDictionary 区别:
- NSArray,NSSet,NSDictionary总结
- 如何为session设置超时时间?
- SAP EWM凭证对象表概览
- NMAP 基础教程
- matlab中mxGetScalar,mxGetPr,mxGetM,mxGetN,mxCreateDoubleMatrix的使用
- IDFA ASIdentifierManager 被拒问题整理分析
- 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber
- MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
- pm_suspend流程
- hibernate 调用存储过程
- Android中SQLite之SQLiteOpenHelper详解
- PHP+MySQL存储数据出现中文乱码的问题
- 对联一幅---诞旦的忧伤
- zoj 1115 Digital Roots
- 充满荆棘的专家程序员之道