如何在NSArray中搜索?

Uni*_*ver 2 iphone objective-c ios4 xcode4.2

我有一个像休息的阵列,

NSArray*array = [NSArray arrayWithObjects:@"1.1 something", @"1.2 something else", @"1.3 out of left field", @"1.4 yet another!", nil];
Run Code Online (Sandbox Code Playgroud)

现在,我有一个像休息一样的字符串,

NSString*str = @"1.3";
Run Code Online (Sandbox Code Playgroud)

现在我将发送str.然后它需要在数组中找到str并且它需要返回该文本找到的对象的索引.我需要索引2的内容必须作为输出.任何人都可以共享代码.谢谢预先.

zap*_*aph 8

这是一个使用块的示例,请注意方法: hasPrefix:

NSArray *array = [NSArray arrayWithObjects:@"1.1 problem1", @"1.2 problem2", @"1.3 problem3", @"1.4 problem4", nil];
NSString *str = @"1.3";

NSUInteger index = [array indexOfObjectPassingTest:
                    ^(id obj, NSUInteger idx, BOOL *stop) {
                        return [obj hasPrefix:str];
                    }];

NSLog(@"index: %lu", index);
Run Code Online (Sandbox Code Playgroud)

NSLog输出:

index: 2
Run Code Online (Sandbox Code Playgroud)