将一些查询爱带给 Objective-C
随时打开问题(功能请求)、Fork,以及/或打开 Pull Request!
(MIT 许可,详细信息见文件底部或 MIT-License.txt 文件。)
NSArray
NSMutableArray
NSDictionary
NSSet
- (NSArray*)take:(NSUInteger)inCount;
返回从目标数组开始指定数量的元素的数组。
// Example - Getting the first five elements
NSArray* elements = [people take:5];
- (NSArray*)skip:(NSUInteger)inCount;
跳过数组中指定的元素数量,并返回剩余元素组成的数组。
// Example - skips the first five elements
NSArray* remaining = [people skip:5];
- (NSArray*)skip:(NSUInteger)inSkip take:(NSUInteger)inTake;
简单方便的方法,结合了 skip 和 take 方法。适用于分页。
// Example - get elements 6-10
NSArray* remaining = [people skip:5 take:5];
- (NSArray*)where:(BOOL(^)(id obj))check;
遍历数组中的每个元素,使用 check
块来确定它们是否应返回到返回数组中。
// Example - find people that are 25 years old
NSArray* 25YearOlds = [people where:^(id obj){ return [obj age] == 25; }];
- (BOOL)any:(BOOL(^)(id obj))check;
检查数组中的每个元素,看是否有任何元素成功通过 check
块。如果没有通过,返回 NO
,否则返回 YES
。
// Example - check to see if anyone is under 18
BOOL containsMinors = [people any:^(id obj){ return [obj age] < 18; }];
- (BOOL)all:(BOOL(^)(id obj))check;
检查数组中的每个元素,看是否有所有元素成功通过 check
块。如果所有元素都通过,则返回 YES
,否则返回 NO
。
// Example - check to see if everyone is 25 or over
BOOL everyone25orOver = [people all:^(id obj){ return [obj age] >= 25; }];
- (NSDictionary*)groupBy:(id(^)(id obj))groupBlock;
使用 groupBlock
块返回的对象作为键,将对象分组到一个包含具有相同键的所有对象的 NSArray 的 NSDictionary 中。
// Example - group everyone by their last name
NSDictionary* peopleByFamilyName = [people groupBy:^(id obj){ return [obj lastName]; }];
- (NSArray*)distinctObjectsByAddress;
进行简单的指针地址比较,以删除指向相同对象的面元素。
// Example - remove the exact same elements
NSArray* uniquePeople = [people distinctObjectsByAddress];
- (NSArray*)distinct;
使用类的 compare 和 hash 方法来删除包含相同值的元素。
// Example - remove the exact same elements
NSArray* uniquePeople = [people distinct];
- (NSArray*)select:(id(^)(id originalObject))transform;
将数组中的元素转换为另一个强类型对象,该对象从transform
块中返回。
// Example - Change people objects into American Class objects
NSArray* americans = [people select:^(id obj){ return [[American alloc] initWithFirstName:[obj firstName]
LastName:[obj lastName]
age:[obj age]] }];
- (NSArray*)selectKeypaths:(NSString*)keypath, ... NS_REQUIRES_NIL_TERMINATION;
使用键路径机制从数组中的元素中选取属性。可以指定任意数量的键路径,但列表必须以nil
结尾。返回值是一个字典数组。该字典以作为参数传入的键路径作为键,以作为值。
// Example - Get the four properties we need from the person object
NSArray* americans = [people selectKeypaths:@"firstName",@"lastName",@"parent.firstName",@"age",nil];
- (id)firstObject;
返回数组中的第一个对象。如果数组为空,则返回nil。
// Example - Get the first person
id person = [people firstObject];
- (id)secondObject;
返回数组中的第二个对象。如果数组不包含两个对象,则返回nil。
// Example - Get the second person
id person = [people secondObject];
- (id)popObjectAtIndex:(NSUInteger)inIndex;
- (id)popFirstObject;
- (id)popLastObject;
移除并返回数组中的对象。如果索引超出了数组的范围,则抛出NSRangeException
。
// Example - remove the first person from the array
Person* firstPerson = [people popFirstObject];
// Example - remove the second person from the array
Person* secondPerson = [people popObjectAtIndex:1];
// Example - remove the last person from the array
Person* lastPerson = [people popLastObject];
- (NSDictionary*)where:(BOOL(^)(id key, id value))check;
对字典中的每个键-对象使用check
块来确定它们是否应包含在返回数组中
// Example - get all the keys and objects where the key is 3 or less characters
NSDictionary* entriesWithKeysOf3OrLess = [peopleGroup where:^(id key, id value){ return [key length] <= 3; }];
- (BOOL)any:(BOOL(^)(id key, id value))check;
检查字典中的每个键-对象,以查看如果有任何元素成功通过check
块。如果没有元素通过,则返回NO
,否则返回YES
。
// Exmaple - finds out if any of the keys are longer than 10 characters
BOOL areAnyKeysLongerThan10 = [peopleGroups any:^(id key, id value){ return [key length] > 10 }];
- (BOOL)all:(BOOL(^)(id key, id value))check;
检查字典中的每个键-对象,以查看是否所有元素都成功通过check
块。如果所有元素都通过,则返回YES
,否则返回NO
。
// Exmaple - find out if all the keys are strings
BOOL areKeysStrings = [peopleGroups all:^(id key, id value){ return [key class] == [NSString class] }];
- (NSSet*)where:(BOOL(^)(id obj))check;
对集合中的每个元素使用check
块来确定它们是否应包含在返回集合中
// Example - find people that are 25 years old
NSSet* 25YearOlds = [people where:^(id obj){ return [obj age] == 25; }];
- (BOOL)any:(BOOL(^)(id obj))check;
检查集合中的每个元素,以查看是否有任何元素成功通过check
块。如果没有元素通过,则返回NO
,否则返回YES
。
// Example - check to see if anyone is under 18
BOOL containsMinors = [people any:^(id obj){ return [obj age] < 18; }];
- (BOOL)all:(BOOL(^)(id obj))check;
检查集合中的每个元素,以查看是否所有元素都成功通过check
块。如果所有元素都通过,则返回YES
,否则返回NO
。
// Example - check to see if everyone is 25 or over
BOOL everyone25orOver = [people all:^(id obj){ return [obj age] >= 25; }];
- (NSDictionary*)groupBy:(id(^)(id obj))groupBlock;
使用groupBlock
块返回的对象作为键,将对象分组到包含所有返回相同键的对象的NSSet字典中
// Example - group everyone by their last name
NSDictionary* peopleByFamilyName = [people groupBy:^(id obj){ return [obj lastName]; }];
- (NSSet*)select:(id(^)(id originalObject))transform;
将集合中的元素转换为另一个强类型对象,该对象从transform
块中返回。
// Example - Change people objects into American Class objects
NSSet* americans = [people select:^(id obj){ return [[American alloc] initWithFirstName:[obj firstName]
LastName:[obj lastName]
age:[obj age]] }];
- (NSSet*)selectKeypaths:(NSString*)keypath, ... NS_REQUIRES_NIL_TERMINATION;
使用键值机制从集合中的元素中选择属性。可以指定任意数量的键值,但列表必须以 nil
结尾。返回值是一个字典集合。这个字典包含作为参数传入的键值作为键,以及 valueForKeyPath: 作为值。
// Example - Get the four properties we need from the person object
NSSet* americans = [people selectKeypaths:@"firstName",@"lastName",@"parent.firstName",@"age",nil];
由 Adam Burkepile 创建于 2012 年 5 月 2 日。
版权所有(C)2012 Adam Burkepile。
特此授予任何获得此软件及其相关文档副本(统称为“软件”)的人,免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得该软件的人为本目的使用该软件,前提是在以下条件下:
以上版权声明和本许可声明应包含在软件的所有副本或实质性部分的副本中。
本软件按“现状”提供,并不提供任何明示或暗示的保证,包括但不限于适销性、特定目的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何人因使用、滥用或与之相关的软件或其使用或其它处置方式引起的任何索赔、损失或其他责任负责,无论该责任是基于合同、侵权或其他原因。