Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

What I want to do is to return the NSArray objects of a specific value (numbers). How can I do it?

Thanks in Advance

share|improve this question
    
return @[ 1, 2, 42, 1337 ]; –  user529758 Jun 14 '13 at 22:29
    
And where's the question about Xcode? –  user529758 Jun 14 '13 at 22:29
    
@H2CO3, those need to be NSNumber literals. –  Matt Wilding Jun 14 '13 at 22:36
    
@MattWilding Correct, thanks, of course. Maybe I should go to bed. –  user529758 Jun 14 '13 at 22:38

1 Answer 1

You can use indexesOfObjectsPassingTest: on your NSArray instance.

NSArray *array = @[1, 2, 3];

typedef BOOL (^Predicate)(id obj, NSUInteger idx, BOOL *stop);
Predicate predicate = ^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[NSNumber class]]) {
        *stop = YES;
        return YES;
    } else return NO;
};

NSIndexSet *indexes = [array indexesOfObjectsPassingTest:predicate];

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/doc/uid/20000137-SW24

share|improve this answer
    
Thank you, but what I need (I did not explain it properly)is, for example,return any type of number –  Alrro Jun 16 '13 at 15:34
    
@Alrro I updated the answer. You can probably use isKindOfClass: or you might have to write a more exhaustive test, but the mechanism is the same. –  trdarr Jun 17 '13 at 22:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.