I have one NSArray
with names in string objects like this:@[@"john", @"smith", @"alex",
@"louis"]
, and I have another array that contains lots of names. How can I check that all the objects in the first array are in the second?
问题:
回答1:
NSSet
has the functionality that you are looking for.
If we disregard performance issues for a moment, then the following snippet will do what you need in a single line of code:
BOOL isSubset = [[NSSet setWithArray: array1] isSubsetOfSet: [NSSet setWithArray: mainArray]];
回答2:
Use this code..
NSArray *temp1 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Jac", nil];
NSArray *temp2 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Rob", nil];
NSMutableSet *telephoneSet = [[NSMutableSet alloc] initWithArray:temp1] ;
NSMutableSet *telephoneSet2 = [[NSMutableSet alloc] initWithArray:temp2];
[telephoneSet intersectSet:telephoneSet2];
NSArray *outPut = [telephoneSet allObjects];
NSLog(@"%@",outPut);
output array contains..
"john","smith","alex","loui
as per ur requirement.
回答3:
Run a loop and use isEqualToStiring
to verify whether array1 objects exists in mainArray.
回答4:
int num_of_matches = 0;
for(NSString *name in mainArray)
{
if(array1 containsObject:name){
num_of_matches++;
}
}
if(num_of_matches == [array1 count]{
// All objects present
}else {
// Matched number is equal of number_of_matches
}
回答5:
If you just need to check if all objects from array1 are in mainArray, you should just use NSSet
e.g.
BOOL isSubset = [[NSSet setWithArray:array1] isSubsetOfSet:[NSSet setWithArray:mainArray]]
if you need to check which objects are in mainArray, you should take a look at NSMutableSet
NSMutableSet *array1Set = [NSMutableSet setWithArray:array1];
[array1Set intersectSet:[NSSet setWithArray:mainArray]];
//Now array1Set contains only objects which are present in mainArray too
回答6:
Use NSArray filteredArrayUsingPredicate: method. Its really fast to find out similar types of object in both arrays
NSPredicate *intersectPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", otherArray];
NSArray *intersectArray = [firstArray filteredArrayUsingPredicate:intersectPredicate];
From above code intersect array gives you same objects which are in other array.
回答7:
Try this way;
NSArray *mainArray=@[@"A",@"B",@"C",@"D"];
NSArray *myArray=@[@"C",@"x"];
BOOL result=YES;
for(id object in myArray){
if (![mainArray containsObject:object]) {
result=NO;
break;
}
}
NSLog(@"%d",result); //1 means contains, 0 means not contains
回答8:
You can use the concept of [NSArray containsObject:]
, where your objects will be from your array1 like you say "john","smith","alex","loui"
回答9:
NSArray *array1 = [NSArray arrayWithObjects:@"a", @"u", @"b", @"v", @"c", @"f", nil];
NSMutableArray *mainArray = [NSMutableArray arrayWithObjects:@"a", @"u", @"I", @"G", @"O", @"W",@"Z",@"C",@"T", nil];
int j=0;
for(int i=0; i < mainArray.count; i++)
{
if (j < array1.count)
{
for( j=0; j <= i; j++)
{
if([[mainArray objectAtIndex:i] isEqualToString:[array1 objectAtIndex:j]] )
{
NSLog(@"%@",[mainArray objectAtIndex:i]);
}
}
}
}