CocoaRouge 0.0.3

CocoaRouge 0.0.3

测试已测试
Lang语言 Obj-CObjective C
许可 MIT
发布最后发布2014年12月

未填写维护。



  • 作者:
  • Arik Devens

Ruby灵感类以提高Objective-C的易用性。

使用

语言构造

unless()

相当于if (!条件),如果条件为假则运行unless中的代码,如果为真则运行else中的代码。

unless (1 != 1) {
    NSLog(@"This will be run because the conditional resolves to NO")
} else {
    NSLog(@"This would be run if the conditional resolved to YES")
}

NSArray

map

返回由每次对块调用的返回值组成的数组。

NSArray *words = @[@"funny", @"little", @"frog"];

NSArray *mappedArray = [words map:id^(id item) {
    NSString *word = (NSString *)item;

    return @"yes";
}];

mappedArray == @[@"yes", @"yes", @"yes"]

join和join

通过将数组的元素连接起来创建一个字符串,可以使用空格或指定的分隔符。

NSArray *words = @[@"funny", @"little", @"frog"];

[words join]; == @"funny little frog"
[words join:@"-"]; == @"funnny-little-frog"

isEmpty

如果数组没有元素则返回true,如果有任一元素返回false。

NSArray *words = @[@"funny", @"little", @"frog"];
[words isEmpty]; == NO

NSArray *noWords = @[]; 
[noWords isEmpty]; == YES

any

如果数组包含通过块进行测试的元素,则返回true。如果没有通过测试的元素,返回false。

NSArray *words = @[@"funny", @"little", @"frog"];

[words any:BOOL^(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"funny"];
}]; == YES

[words any:BOOL^(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"belle"];
}]; == NO

select

返回数组中的元素,这些元素通过块中的测试。

NSArray *words = @[@"funny", @"little", @"frog"];

NSArray *selectedWords = [words select:^BOOL(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"funny"];
}]; == @[@"funny"]

selectedWords = [words select:^BOOL(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"belle"];
}]; == @[]

reject

返回数组中任何未通过块中测试的元素。

NSArray *words = @[@"funny", @"little", @"frog"];

NSArray *selectedWords = [words reject:^BOOL(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"funny"];
}]; == @[@"little", @"frog"]

selectedWords = [words reject:^BOOL(id item) {
    NSString *word = (NSString *)item;

    return [word isEqualToString:@"belle"];
}]; == @[@"funny", @"little", @"frog"]

each

为数组中的每个元素执行一个块。

NSArray *words = @[@"funny", @"little", @"frog"];

[words each:^(id item) {
    NSLog(@"This will be called once for each item in words");
}];

each_with_index

为数组中的每个元素执行一个块,并且传入了元素的索引。

NSArray *words = @[@"funny", @"little", @"frog"];

[words eachWithIndex:^(id item, int index) {
    NSLog(@"This will be called once for each item in words and pass the index of the item");
}];

NSNumber

times

根据数字的值执行块多次。

[@(4) times:{
    NSLog(@"This block will be called 4 times.")
}];

timesWithIndex

根据数字的值执行块多次,并传入当前迭代的索引。

[@(4) times:^(int i) {
    NSLog(@"This block will be called 4 times and will pass the count each time")
}];

toString

将NSNumber转换为NSString。

[@(42) toString]; == 42

NSString

join:和join:

通过在两个字符串之间添加空格或指定的分隔符来连接两个字符串。

[[@"funny" join:@"little"] join:@"frog"]; == @"funny little frog"

[[@"funny" join:@"little" with:@"-"] join:@"frog" with:@"-"]; == @"funny-little-frog"

isEmpty

如果字符串长度为0则返回true,如果大于0则返回false。

NSString *text = @"";   
[text isEmpty]; == YES

NSString *text = @"funny little frog";
[text isEmpty]; == NO

chomp和chomp

从字符串末尾移除指定的字符串,如果没有指定字符串则移除\n。

NSString *text = @"funny little frog\n";
[text chomp]; == @"funny little frog"   
[text chomp:@"frog"]; == @"funny little "

toNum

将NSString转换为NSNumber。

[@"42" toNum]; == @(42)

gsub:with

将匹配的字符串替换为指定的字符串。

[@"Adam" gsub:@"Adam" with:@"Help"]; == "Help"

split和split

返回一个数组,该数组通过空格或指定的分隔符将字符串分割。

[@"funny little frog" split]; == @[@"funny", @"little", @"frog"]
[@"funny-little frog" split:@"-"]; == @[@"funny", @"little frog"]

index

返回另一个字符串中字符串首次出现的索引。

[@"funny little frog" index:@"little"]; == 6
[@"funny little frog" index:@"belle"]; == -1