[翻译] 用 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

  1. Install via CocoaPods

    pod 'ObjectiveSugar'
    
  2. 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);
时间: 2024-11-05 09:28:04

[翻译] 用 ObjectiveSugar 扩展NSArray NSDictionary NSSet NSNumber的相关文章

Objective-C中关于NSArray, NSDictionary, NSNumber等写法的进化

从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性.创建数组NSArray,哈希表NSDictionary, 数值 对象NSNumber时,可以像NSString的初始化一样简单方便.妈妈再也不担心程序写得手发酸了. 有兴趣的朋友可以关注LLVM编译器的相关文档:http://clang.llvm.org/docs/ObjectiveCLiterals.html 关于NSDictionary和NSNumber的例子来自:http://cocoaheads.tum

NSArray与NSSet

NSArray与NSSet NSArray:有序的集合,存储的元素在一个整块的内存中并按序排列: NSSet:无序的集合,散列存储. 读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order of elements isn't important and performance in testing whether an object is contained in the set

Node.js Addons翻译(C/C++扩展)_node.js

PS:请先升级Node 6.2.1,Node 升级命令 npm install -g n;n stable.NOde.js扩展是一个通过C/C++编写的动态链接库,并通过Node.js的函数require()函数加载,用起来就像使用一个普通的Node.js模块.它主要为Node与C/C++库之间提供接口. 这样,若一个方法或函数是通过Node扩展实现则变得相当复杂,涉及几个模块与接口的知识: •v8:一个实现了通过C++库实现了的javascript.V8提供了创建对象机制,回调函数等.V8AP

OC——第五天NSSArray&amp;nbsp;/NSDicti…

1.oc中常用的集合(容器)有: 1.NSArry 数组:2.NSDicitionary 字典:3.NSSet 集(不常用)     数组:NSArry 继承自NSObject ,不可变的数组,用于管理一系列对象的一个有序的集合:注意:数组中只能存放对象类型,虽然数组中存储的内容不能修改,但是数组指针是可以重指向的;   ============================================== 总结:NSArray NSDictionary NSSet     1. NSAr

iOS最新面试锦集

1. 为什么说Objective-C是一门动态的语言? ① 什么是动态语言? 动态语言,是指程序在运行时可以改变其结构:新的函数可以被引进,已有的函数可以被删除等在结构上的变化.比如众所周知的ECMAScript(JavaScript)便是一个动态语言.除此之外如Ruby.Python等也都属于动态语言,而C.C++等语言则不属于动态语言. 有三个名词容易混淆: Dynamic Programming Language (动态语言或动态编程语言) Dynamically Typed Langua

OC19归档

//(main 接口) // ViewController.m // OC19归档 // // Created by Zoujie on 15/11/28. // Copyright 2015年 Zoujie. All rights reserved. // #import "ViewController.h" #import "Foo.h" #import "AddressCard.h" @interface ViewController ()

OC——第五天NSSArray&amp;amp;nbsp;/NSDicti…

1.oc中常用的集合(容器)有: 1.NSArry 数组:2.NSDicitionary 字典:3.NSSet 集(不常用)     数组:NSArry 继承自NSObject ,不可变的数组,用于管理一系列对象的一个有序的集合:注意:数组中只能存放对象类型,虽然数组中存储的内容不能修改,但是数组指针是可以重指向的;   ============================================== 总结:NSArray NSDictionary NSSet     1. NSAr

App的内存优化

这篇文章是笔者在开发App过程中发现的一些内存问题, 然后学习了YYKit框架时候也发现了图片的缓存处理 (YYKit 作者联系了我, 说明了YYKit重写imageNamed:的目的不是为了内存管理, 而是增加兼容性, 同时也是为了YYKit中的动画服务). 以下内容是笔者在开发中做了一些实验以及总结. 如有错误望即时提出, 笔者会第一时间改正. 文章的前篇主要是对两种不同的UIImage工厂方法的分析, 罗列出这些工厂方法的内存管理的优缺点. 文章的后篇是本文要说明的重点, 如何结合两种工厂

iPhone开发内存管理

  开发iPhone 应用程序并不难,基本上就是三个词 - "memory, memory, memory" .iPhone OS 对内存的要求很严格,有memory leak ,杀掉; 内存使用超限额,杀掉.一个经过测试的程序,在使用过程中90%以上的崩溃都是内存问题造成的.在这里简单总结一下Object-C 内存管理. 基本概念 Object-C 的内存管理基于引用计数(Reference Count)这种非常常用的技术.简单讲,如果要使用一个对象,并希望确保在使用期间对象不被释放