iphone数组与字典的索引

发布于 2024-12-19 03:42:42 字数 403 浏览 4 评论 0原文

我有一个带有字典的数组,

.如何获取特定字典的索引...[我在其中获取了我想要查找的字典的数据,但需要打开索引]

NSMutableDictionary *dictois = [[NSMutableDictionary alloc]init];
[dictois setObject:@"easySpritedd" forKey:@"Nombre"];
[dictois setObject:@"X" forKey:@"290"];
[dictois setObject:@"Y" forKey:@"300"];

int fooIndex = [self.bloquesArray indexOfObject: dictois];

但正如你所见,我还不知道如何获取字典以在对象索引

谢谢!

i have an array with dictionaries,

. how to get the index for a specific dictionary... [where I got the data for the dictionary i want to find, but need the index is on]

NSMutableDictionary *dictois = [[NSMutableDictionary alloc]init];
[dictois setObject:@"easySpritedd" forKey:@"Nombre"];
[dictois setObject:@"X" forKey:@"290"];
[dictois setObject:@"Y" forKey:@"300"];

int fooIndex = [self.bloquesArray indexOfObject: dictois];

but as you see i dont know yet how to get the dictionary for comparing in the indexOfObject

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-12-26 03:42:42

NSDictionary对象有以下比较方法:

- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary

有以下讨论

如果两个字典包含相同的数字,则它们具有相同的内容
的条目,以及对于给定的键,相应的值对象
每个字典都满足 isEqual: 测试。

您应该属于这种情况。遗憾的是,您无法更改 NSArray 用于计算 indexOfObject 的方法:因此它无法帮助您,因此 Denis 提供的解决方案是我能想到的最好的解决方案(除非您喜欢子类化 NSArray,但我不推荐)。


作为对 Denis 答案的补充,您可以使用 NSArray 方法

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

而不是

- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

如果数组中有多个相等的对象是可能的。

NSDictionary objects have the following comparison methods:

- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary

With the following discussion

Two dictionaries have equal contents if they each hold the same number
of entries and, for a given key, the corresponding value objects in
each dictionary satisfy the isEqual: test.

This should be the case for you. Sadly you cannot change the method which NSArray uses to calculate the indexOfObject: so it can't help you, hence the solution provided by Denis is the best I can think of (unless you fancy subclassing NSArray which I wouldn't recommend).


Just as a complement to Denis' answer you could use the NSArray method

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

instead of

- (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

If having several equal objects in the array is a possibility.

同尘 2024-12-26 03:42:42

使用以下代码

NSMutableDictionary *dictois = [[NSMutableDictionary new]autorelease];

[dictois setObject:@"easySpritedd" forKey:@"Nombre"];
[dictois setObject:@"X" forKey:@"290"];
[dictois setObject:@"Y" forKey:@"300"];

int fooIndex = [self.bloquesArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop){
  if( [[obj class] isKindOfClass: [NSDictionary class]] ) {
    BOOL result = [obj isEqualToDictionary: diction];
    *stop = result;
    return result;
  }

  *stop = NO;
  return NO;

}];

Use the following code

NSMutableDictionary *dictois = [[NSMutableDictionary new]autorelease];

[dictois setObject:@"easySpritedd" forKey:@"Nombre"];
[dictois setObject:@"X" forKey:@"290"];
[dictois setObject:@"Y" forKey:@"300"];

int fooIndex = [self.bloquesArray indexOfObjectPassingTest: ^(id obj, NSUInteger idx, BOOL *stop){
  if( [[obj class] isKindOfClass: [NSDictionary class]] ) {
    BOOL result = [obj isEqualToDictionary: diction];
    *stop = result;
    return result;
  }

  *stop = NO;
  return NO;

}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文