ios 如何使用字符串值获取 NSArray 中对象的索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9136785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the index of an object in an NSArray using string value?
提问by jroyce
I want to get the index of an object within the NSMutableArray of categories.
我想在 NSMutableArray 的类别中获取对象的索引。
The category object has an attribute "category_title" and I want to be able to get the index by passing the value of category_title.
类别对象有一个属性“category_title”,我希望能够通过传递 category_title 的值来获取索引。
I have looked through the docs and can't find a simple way to go about this.
我已经浏览了文档,但找不到一个简单的方法来解决这个问题。
回答by Conrad Shultz
NSArray
does not guarantee that you can only store one copy of a given object, so you have to make sure that you handle that yourself (or use NSOrderedSet
).
NSArray
不保证您只能存储给定对象的一个副本,因此您必须确保自己处理(或使用NSOrderedSet
)。
That said, there are a couple approaches here. If your category objects implement isEqual:
to match category_title, then you can just use -indexOfObject:
.
也就是说,这里有几种方法。如果您的类别对象实现isEqual:
匹配 category_title,那么您只需使用-indexOfObject:
.
If you can't do that (because the category objects use a different definition of equality), use -indexOfObjectPassingTest:
. It takes a block in which you can do whatever test you want to define your "test" - in this case, testing category_title
string equality.
如果您不能这样做(因为类别对象使用不同的相等定义),请使用-indexOfObjectPassingTest:
. 它需要一个块,您可以在其中执行您想要定义“测试”的任何测试 - 在这种情况下,测试category_title
字符串相等性。
Note that these are all declared for NSArray
, so you won't see them if you are only looking at the NSMutableArray
header/documentation.
请注意,这些都是为 声明的NSArray
,因此如果您只查看NSMutableArray
标题/文档,则不会看到它们。
EDIT: Code sample. This assumes objects of class CASCategory
with an NSString
property categoryTitle
(I can't bring myself to put underscores in an ivar name :-):
编辑:代码示例。这假定类的对象CASCategory
具有NSString
属性categoryTitle
(我不能让自己在 ivar 名称中添加下划线 :-):
CASCategory *cat1 = [[CASCategory alloc] init];
[cat1 setCategoryTitle:@"foo"];
CASCategory *cat2 = [[CASCategory alloc] init];
[cat2 setCategoryTitle:@"bar"];
CASCategory *cat3 = [[CASCategory alloc] init];
[cat3 setCategoryTitle:@"baz"];
NSMutableArray *array = [NSMutableArray arrayWithObjects:cat1, cat2, cat3, nil];
[cat1 release];
[cat2 release];
[cat3 release];
NSUInteger barIndex = [array indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
if ([[(CASCategory *)obj categoryTitle] isEqualToString:@"bar"]) {
*stop = YES;
return YES;
}
return NO;
}];
if (barIndex != NSNotFound) {
NSLog(@"The title of category at index %lu is %@", barIndex, [[array objectAtIndex:barIndex] categoryTitle]);
}
else {
NSLog(@"Not found");
}
回答by Bruce Dean
Not sure that I understand the question but something like this might work (assuming the Mutable Array contains objects of Class "Category"):
不确定我是否理解这个问题,但这样的事情可能会起作用(假设可变数组包含“类别”类的对象):
int indx;
bool chk;
for (Category *aCategory in theArray)
{
chk = ([[aCategory category_title] isEqualToString:@"valOfCategoryTitle"])
if ( chk )
indx = [theArray indexOfObject:aCategory];
}
回答by Leena
Try this code much more simpler:-
试试这个更简单的代码:-
int f = [yourArray indexOfObject:@"yourString"];