sham
sham

Reputation: 35

Any built in functions to check "All elements of an array contains [NSNULL null]" type object or not?

I have an NSMutableArray which holds 100 [NSNULL null] type objects.

However some times it contains 1 valid object(it may be a NSString) and 99 [NSNULL null] type objects(it may vary according to the situations).

may i know is there any built in functions to check ,all elements of array contains [NSNULL null] type object or not?(or it does not contains any one valid objects.)

Thanks.

NB: without iterating all elements using loop statements.

Upvotes: 1

Views: 1711

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

You can use NSPredicate to achieve this.

NSMutableArray *allObjects = /* Assume this is your main array */;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self = nil"];
NSArray *nullObjects = [allObjects filteredArrayUsingPredicate:predicate];

if ([nullObjects count] == [allObjects count]) {
    // All objects are [NSNull null]
} else {
    // Some objects are of different types(may be NSString)
}

Upvotes: 2

Kasper K.
Kasper K.

Reputation: 201

Assuming you know the NSString object you are looking for, you can do it really simple:

NSString *needle = /*The NSString you are looking for*/;
NSMutableArray *allObjects = /*You mutable array*/;

BOOL contains = [allObjects containsObject:needle];

Really simple, but it's assuming that you know the object you are looking for, and since you didn't point that out in the question, this answer might be of help to someone.

Upvotes: 0

Related Questions