gpt4 book ai didi

objective-c - 如何在 NSArray 中找到某种对象?

转载 作者:太空狗 更新时间:2023-10-30 03:40:34 25 4
gpt4 key购买 nike

我的第一直觉是

FooType *myFoo = nil;
for (id obj in myArray) {
if ( [obj isKindOfClass:[FooType class]] ) myFoo = obj;
}

有了 Objective-C 和 NSArray 中的所有好东西,一定有更好的方法,对吧?

最佳答案

使用 Blocks 支持(在 iOS 4 或 Snow Leopard 中):

FooType *myFoo = nil;
NSUInteger index = [myArray indexOfObjectPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop) {
return [obj isKindOfClass:[FooType class]];
}];
if (index != NSNotFound) myFoo = [myArray objectAtIndex:index];

实际上并没有更短。您可能会考虑编写自己的 NSArray 方法来执行此操作。

关于objective-c - 如何在 NSArray 中找到某种对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3540094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com