我有一个NSArray自定义对象都有@property name类型NSString.如何快速枚举数组并创建一个只包含name属性中具有特定单词的对象的新数组?
例如:
CustomObject *firstObject = [[CustomObject alloc] init];
firstObject.name = @"dog";
CustomObject *secondObject = [[CustomObject alloc] init];
secondObject.name = @"cat";
CustomObject *thirdObject = [[CustomObject alloc] init];
thirdObject.name = @"dogs are fun";
NSMutableArray *testArray = [NSMutableArray arrayWithObjects:firstObject,
secondObject,
thirdObject,
nil];
// I want to create a new array that contains all objects that have the word
// "dog" in their name property.
Run Code Online (Sandbox Code Playgroud)
我知道我可以像这样使用for循环:
NSMutableArray *newArray = [NSMutableArray array];
for (CustomObject *obj in testArray) …Run Code Online (Sandbox Code Playgroud)