2

I have NSMutableArray that contains NSArray and String:

NSMutableArray *alphabets = [[NSMutableArray alloc] init];
[alphabets addObject:[NSArray arrayWithObjects:@"ㄱ",@"ㄲ", nil]];
[alphabets addObject:@"ㄴ"];
[alphabets addObject:[NSArray arrayWithObjects:@"ㄷ",@"ㄸ", nil]];
[alphabets addObject:@"ㄹ"];
[alphabets addObject:@"ㅁ"];
[alphabets addObject:[NSArray arrayWithObjects:@"ㅂ",@"ㅃ", nil]];
[alphabets addObject:[NSArray arrayWithObjects:@"ㅅ",@"ㅆ", nil]];
[alphabets addObject:@"ㅇ"];
[alphabets addObject:[NSArray arrayWithObjects:@"ㅈ",@"ㅉ", nil]];
[alphabets addObject:@"ㅊ"];
[alphabets addObject:@"ㅋ"];
[alphabets addObject:@"ㅌ"];
[alphabets addObject:@"ㅍ"];
[alphabets addObject:@"ㅎ"];

I want to check if object count by index is bigger than 1 then I can know my alphabet has contains arrays that has multiple values. Here is the code:

for (int i = 0 ; i < [alphabets count]; i++){
    if([[alphabets objectAtIndex:i] count] > 1){
         NSLog(@"multiple values");
    }else{
            NSLog(@"%@", [alphabets objectAtIndex:i]);
    }    
}

But the code is always get errors.

This is the error: unrecognized selector sent to instance 0x2d550

How to count objects by index? Please help me to solve it.

Thank

1
  • What you need to do is determine if an element of your outer array is itself an array: if ([alphabets[i] isKindOfClass:[NSArray class]]). Commented Dec 15, 2013 at 4:38

4 Answers 4

2

You cannot send the message count to NSString. Since you have a mix of NSString and NSArray objects in your array, you need to add a check to see that you are sending count only to NSArrays, not to NSStrings:

for (int i = 0 ; i < [alphabets count]; i++){
    if([[alphabets objectAtIndex:i] isKindOfClass:[NSArray class]] && [alphabets objectAtIndex:i].count > 1){
         NSLog(@"multiple values");
    }else{
         NSLog(@"%@", [alphabets objectAtIndex:i]);
    }    
}
Sign up to request clarification or add additional context in comments.

Comments

2

Here's one approach, which uses fast enumeration:

for (NSObject *object in alphabets) {
    if ([object respondsToSelector:@selector(count)]) {
        // It's probably an array
        NSArray *array = (NSArray *)object;
        NSLog(@"multiple values in array with count %d", array.count);
    } else {
        NSLog(@"%@", object);
    }
}

This will prevent sending count to the wrong object.

One word of caution: respondsToSelector: only checks that one selector, and other objects may respond to count but not be arrays. (NSDictionary is one example.) In this case you could use other similar tests, like isKindOfClass:.

2 Comments

It's a bit daft to check whether an object implements "count" to decide that it is an NSArray. Would be much better to check whether it is an NSArray to decide it is an NSArray. Like [object isKindOfClass:[NSArray class]].
@gnasher729 You're right: isKindOfClass: is safer. I think I was trying to show the OP one way to debug "unrecognized selector" errors, but that doesn't really come across in my answer.
1

If all you want to do is check if the object is an array, just use

if([something isKindOfClass:[NSArray class]]){
    NSLog(@"It's an array");
}

Comments

0

Your NSMutableArray contains two different kinds of objects: NSString objects and NSArray objects. So everyone who uses objects coming from this array has to check whether the object they've got is an NSString or an NSArray.

It might be easier for the rest of your code if you add NSArray objects with exactly one NSString instead of adding plain NSString objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.