Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何获取对象索引?_Iphone_Ios_Indexing_Nsmutablearray_Nsarray - Fatal编程技术网

Iphone 如何获取对象索引?

Iphone 如何获取对象索引?,iphone,ios,indexing,nsmutablearray,nsarray,Iphone,Ios,Indexing,Nsmutablearray,Nsarray,如何获取对象索引?我有一个包含数组的字典,在数组中我有多个字典 我的数据结构(例如): 学生----NSDictionary 有人能帮忙吗?非常感谢你 在代码中,您忘记了创建somedict和somearray。问题可能就在那里 另外,您不需要在数组中分配namevalue一个空字典,然后分配实际字典 检查此工作代码片段: NSUInteger idx; NSDictionary *john = [NSDictionary dictionaryWithObjectsAndKeys:@"John"

如何获取对象索引?我有一个包含数组的字典,在数组中我有多个字典

我的数据结构(例如):

学生----NSDictionary


有人能帮忙吗?非常感谢你

在代码中,您忘记了创建
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:
    方法需要比较属性的值
  • 上面的代码不包括有许多具有相同值的对象的情况(就像许多同名的学生!)
我希望我的“答案”能让你更容易实现你所需要的。

谢谢你的回答(:somedict是一本字典,在我的代码中有值。我只是在这里添加了一段重要代码(:我不知道“namevalue”)我想根据用户输入查找索引数组的名称。这就是为什么我想知道是否有方法通过“namevalue”检索对象索引(: