我找不到一種方法使用NSPredicate
字符串獲取數組中的第012項項目中的第個項目。例如:NSPredicate String獲得第n個項目
//You cannot touch or modify the code inside this method. You can only use the predicate string param to filter the array.
- (NSArray *)filterUsingNSPredicate:(NSString *)PredicateString
{
NSArray *array = @[
@"firstItem",
@"secondItem",
@"thirdItem",
@"fourthItem",
];
NSPredicate *pred = [NSPredicate predicateWithFormat:PredicateString];
NSArray *filtered = [array filteredArrayUsingPredicate:pred];
NSLog(@"This is the second item in the array! %@",filtered); //Thats the only thing in the array.
return filtered;
}
有沒有您想要使用的謂詞字符串,而不只是'陣列的理由[indexYouWant]'或'[array objectAtIndex:indexYouWant]'? – AdamPro13
是的,我在做什麼,我沒有對數組本身的引用,我只能使用NSPredicateString進行查詢。在這種情況下,我只是將過濾後的數組返回給我。 – Rafthecalf
您可以將該數組存儲在臨時數組中並使用'objectAtIndex'獲取第n個元素 – Lion