【发布时间】:2024-05-20 22:30:02
【问题描述】:
我有一个array,里面填满了NSDictionaries。我想找到其中一本字典的index,但我对这本字典的了解只是value 的键@"name".
我该怎么做 ?
【问题讨论】:
标签: iphone objective-c ios xcode nsarray
我有一个array,里面填满了NSDictionaries。我想找到其中一本字典的index,但我对这本字典的了解只是value 的键@"name".
我该怎么做 ?
【问题讨论】:
标签: iphone objective-c ios xcode nsarray
NSArray *temp = [allList valueForKey:@"Name"];
NSInteger indexValue = [temp indexOfObject:YourText];
NSString *name = [[allList objectAtIndex:indexValue] valueForKey:@"Name"]
【讨论】:
在theArray 中查找@"name" 的值为theValue 的第一个字典的索引:
NSUInteger index = [theArray indexOfObjectPassingTest:
^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
{
return [[dict objectForKey:@"name"] isEqual:theValue];
}
];
如果没有找到匹配的对象,index 将是 NSNotFound。
【讨论】:
indexOfObjectPassingTest更有效。
NSNotFound。