В вашем коде вы забыли включить создание 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 */
Метод indexOfObject
будет использовать isEqual:
для сравнения объектов.Вы можете убедиться, что новый объект будет считаться равным объекту в массиве:
NSLog(@"jane is maryjane? %i", [jane isEqual:maryjane]); /* 1 */