Iphone 如何获取对象索引?
如何获取对象索引?我有一个包含数组的字典,在数组中我有多个字典 我的数据结构(例如): 学生----NSDictionaryIphone 如何获取对象索引?,iphone,ios,indexing,nsmutablearray,nsarray,Iphone,Ios,Indexing,Nsmutablearray,Nsarray,如何获取对象索引?我有一个包含数组的字典,在数组中我有多个字典 我的数据结构(例如): 学生----NSDictionary 有人能帮忙吗?非常感谢你 在代码中,您忘记了创建somedict和somearray。问题可能就在那里 另外,您不需要在数组中分配namevalue一个空字典,然后分配实际字典 检查此工作代码片段: NSUInteger idx; NSDictionary *john = [NSDictionary dictionaryWithObjectsAndKeys:@"John"
有人能帮忙吗?非常感谢你 在代码中,您忘记了创建
somedict
和somearray
。问题可能就在那里
另外,您不需要在数组中分配namevalue
一个空字典,然后分配实际字典
检查此工作代码片段:
NSUInteger idx;
NSDictionary *john = [NSDictionary dictionaryWithObjectsAndKeys:@"John", @"name",
[NSNumber numberWithInt:23], @"age", nil];
NSDictionary *jane = [NSDictionary dictionaryWithObjectsAndKeys:@"Jane", @"name",
[NSNumber numberWithInt:24], @"age", nil];
NSArray *students = [NSArray arrayWithObjects:john, jane, nil];
idx = [students indexOfObject:john];
NSLog(@"john is at: %i", idx == NSNotFound ? -1 : idx); /* 0 */
idx = [students indexOfObject:jane];
NSLog(@"jane is at: %i", idx == NSNotFound ? -1 : idx); /* 1 */
现在,尝试使用数组中不存在的对象:
NSDictionary *mary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mary", @"name",
[NSNumber numberWithInt:22], @"age", nil];
idx = [students indexOfObject:mary];
NSLog(@"mary is at: %i", idx == NSNotFound ? -1 : idx); /* -1 Not found */
NSDictionary *maryjane = [NSDictionary dictionaryWithObjectsAndKeys:@"Jane", @"name",
[NSNumber numberWithInt:24], @"age", nil];
idx = [students indexOfObject:maryjane];
NSLog(@"maryjane is at: %i", idx == NSNotFound ? -1 : idx); /* 1 */
NSLog(@"jane is maryjane? %i", [jane isEqual:maryjane]); /* 1 */
最后是一个新对象,但创建为数组中已存在对象的精确副本:
NSDictionary *mary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mary", @"name",
[NSNumber numberWithInt:22], @"age", nil];
idx = [students indexOfObject:mary];
NSLog(@"mary is at: %i", idx == NSNotFound ? -1 : idx); /* -1 Not found */
NSDictionary *maryjane = [NSDictionary dictionaryWithObjectsAndKeys:@"Jane", @"name",
[NSNumber numberWithInt:24], @"age", nil];
idx = [students indexOfObject:maryjane];
NSLog(@"maryjane is at: %i", idx == NSNotFound ? -1 : idx); /* 1 */
NSLog(@"jane is maryjane? %i", [jane isEqual:maryjane]); /* 1 */
方法indexOfObject
将使用isEqual:
来比较对象。您可以验证新对象是否与数组中的对象相等:
NSDictionary *mary = [NSDictionary dictionaryWithObjectsAndKeys:@"Mary", @"name",
[NSNumber numberWithInt:22], @"age", nil];
idx = [students indexOfObject:mary];
NSLog(@"mary is at: %i", idx == NSNotFound ? -1 : idx); /* -1 Not found */
NSDictionary *maryjane = [NSDictionary dictionaryWithObjectsAndKeys:@"Jane", @"name",
[NSNumber numberWithInt:24], @"age", nil];
idx = [students indexOfObject:maryjane];
NSLog(@"maryjane is at: %i", idx == NSNotFound ? -1 : idx); /* 1 */
NSLog(@"jane is maryjane? %i", [jane isEqual:maryjane]); /* 1 */
如果我理解正确,您正在寻找一种基于对象所具有的属性值在NSDictionary中查找对象索引的方法,对吗?
例如,您的
students
词典中有一个名为Grace
的学生,您想知道名为Grace
的学生对象存储在哪个索引中
如果上面是真的,下面是我的(不完整和粗略的)解决方案,它只对具有NSString
属性的对象有效,但是一旦你了解了下面的想法,我想你就可以修改代码来满足你的需要了
- (NSInteger)indexOfObjectWithPropertyValue:(id)value inDictionary:(NSDictionary *)dict {
NSInteger objectIndex = 0;
unsigned int outCount, i;
for (id key in dict) {
id objForKey = [dict objectForKey:key];
Class lender = [objForKey class];
objc_property_t *properties = class_copyPropertyList(lender, &outCount);
for (i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
id v = [objForKey valueForKey:[NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding]];
if ([v isKindOfClass:[value class]] && [v isEqual:value]) {
return objectIndex;
}
}
objectIndex++;
}
return NSNotFound; // i.e. NSIntegerMax
}
-(NSInteger)indexOfObjectWithPropertyValue:(id)value indicationary:(NSDictionary*)dict{
NSInteger objectIndex=0;
无符号整数超过,i;
for(dict中的id键){
id objForKey=[dict objectForKey:key];
贷款人类别=[objForKey类别];
objc_property_t*properties=class_copyPropertyList(贷方和余额);
对于(i=0;i
不要忘记包含运行时标题:
#import <objc/runtime.h>
#导入
我的代码可能存在一些问题:
- 最明显的
方法需要比较属性的值isEqual:
- 上面的代码不包括有许多具有相同值的对象的情况(就像许多同名的学生!)