[翻译] 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber

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.. 4 NSDate *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 +0000
newYearsDay之前的第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备份
他们没有修改原始的array NSArray *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 objects NSArray *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, 7 cars.sample
// 458 Italia
cars.sample
// F50
NSArray only
NSArray 单独的
NSArray *numbers = @[@1, @2, @3, @4, @5, @6];
从2到4
// index from 2 to 4
numbers[@"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: 4
numbers[@"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-c NSArray *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: 3
NSDictionary *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);
[翻译] 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber的更多相关文章
- [转]一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- Fouandation(NSString ,NSArray,NSDictionary,NSSet) 中常见的理解错误区
Fouandation 中常见的理解错误区 1.NSString //快速创建(实例和类方法) 存放的地址是 常量区 NSString * string1 = [NSString alloc]init ...
- 一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- 遍历NSArray, NSDictionary, NSSet的方法总结
1,for循环读取 NSArray: NSArray *array = /*…*/ ; i<array.count; i++) { id object = array[i]; // do sth ...
- Read and Write NSArray, NSDictionary and NSSet to a File
查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset- ...
- 在Objective-C 中使用字符生成NSArray、NSDictionary、NSNumber
@符号不仅可以生成字符串,还可以生成其他数据类型如NSArray.NSDictionary和NSNumber,是一种简洁快速的用法. // NSArray array = [NSArray array ...
- Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法
Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...
- Objective-C中关于NSArray, NSDictionary, NSNumber等写法的进化
从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性.创建数组NSArray,哈希表NSDictionary, 数值对象NSNumber时,可以像NSString的初始 ...
- NSData NSDate NSString NSArray NSDictionary 相互转换
// NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...
随机推荐
- Spark(十一)Spark分区
一.分区的概念 分区是RDD内部并行计算的一个计算单元,RDD的数据集在逻辑上被划分为多个分片,每一个分片称为分区,分区的格式决定了并行计算的粒度,而每个分区的数值计算都是在一个任务中进行的,因此任务 ...
- 【AtCoder】AGC011 D - Half Reflector
题解 大意是n个管子排成一排,每个管子有两种状态,A状态是从某个方向进去,从原方向出来,B状态是从某个方向进去,从另一个方向出来 球经过一个A状态的管子这个管子会立刻变成B状态,经过一个B状态的管子会 ...
- 004 Hadoop2.x基础知识
一:大数据应用 1.Cloudera cloudera公司是Hadoop三大发行商之一,其版本为CDH版本,现在最新的版本是CDH5. 网站:http://archive.cloudera.com/c ...
- Bootstrap进阶三:jQuery插件概览
单个还是全部引入 JavaScript 插件可以单个引入(使用 Bootstrap 提供的单个 *.js 文件),或者一次性全部引入(使用 bootstrap.js 或压缩版的 bootstrap.m ...
- R语言实战(七)图形进阶
本文对应<R语言实战>第11章:中级绘图:第16章:高级图形进阶 基础图形一章,侧重展示单类别型或连续型变量的分布情况:中级绘图一章,侧重展示双变量间关系(二元关系)和多变量间关系(多元关 ...
- SQL注入备忘录
备忘录(一) 拿起小本本记下常考知识点. 常用连接词 and && %23%23 且 or || %7c%7c 或 xor 非 Access 数据库: 只能爆破表名.列名获取数据.无法 ...
- ubuntu16.04服务器apache的ssl证书配置
背景:在腾讯云申请的免费证书1年,服务器ubuntu 16.04版本,我的是多域名 1.ssl模块的安装 sudo a2enmod ssl //开启apache ssl模块 a2ensite defa ...
- android 安全退出 activity
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 定义一个 活动 的基础类, 每次打开一个 活动,就记录下来. 退出时,关闭每一个 活动. ...
- 【贪心】Codeforces Round #480 (Div. 2) C. Posterized
题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...
- 20162303 解读同伴的收获&解决同伴的问题 周三补交
解读同伴的收获&解决同伴的问题 11月29号 解决同伴的问题 我的同组同学是20162307学号张韵琪同学 同组同学的问题是动态规划算法步骤中递归定义的最优值 我理解他的意思是她不太理解最优值 ...