On Nov 28, 2008, at 8:25 PM, Pierce Freeman wrote:

I am attempting to check one array's contents against another's, but so far
it hasn't been working.  In the actual application, I am checking the
current open applications against an "okay" application list, and just
thought of another problem: If the user doesn't have an application open that is on the list, it will think the reverse. Some example code (with
different variables) is below:

[CODE]
for (int arraySort = 0; arraySort < [arrayInfo count]; arraySort++)
           {
               for (int arrayNewSort = 0; arrayNewSort < [arrayNewInfo
count]; arrayNewSort++)
               {
                   if ([ arrayInfo objectAtIndex:arraySort] ==
[arrayNewInfo objectAtIndex:arrayNewSort])
                   {
                       some++;
                   }
               }
           }

If (some == [arrayNewInfo count]
{
   NSLog(@"The user has only the okay applications open");
}
[END CODE]


First of all, I'd recommend using the method isEqual: to check one object against another. It's entirely possible to have two objects that are separate instances and which have different pointers but are essentially equal, for example two NSStrings that have the same exact content but are separate instances. isEqual: checks if the values of the objects are actually equal and it is the preferred way to check two objects for equality.

Secondly you can use the containsObject: method of NSArray to get rid of one of your loops. I'd rather let NSArray use its internal, possibly optimized method rather than doing it myself.

Lastly, you really should use Enumeration to run through a collection. If you are targeting Mac OS before 10.5 then use regular enumeration, if you are targeting Mac OS 10.5 or later then Fast Enumeration is a nice shortcut.

Here's an example using NSArray and Fast Enumeration:

NSArray *openApps = [NSArray arrayWithObjects:@"one",@"two",@"three",nil]; NSArray *allowedApps = [NSArray arrayWithObjects:@"one",@"two",@"three",@"four",nil];
        BOOL allAllowed = YES;
        
        for(id anItem in openApps)
                allAllowed = allAllowed && [allowedApps containsObject:anItem];
        
        if(allAllowed)
                NSLog(@"The user has only the okay applications open");
        else
                NSLog(@"The user has non-allowed applications open");

This is even easier (and probably faster) if you use NSSet and the NSSet method isSubsetOfSet:

        NSSet *openSet = [NSSet setWithObjects:@"one",@"two",@"three",nil];
NSSet *allowedSet = [NSSet setWithObjects:@"one",@"two",@"three",@"four",nil];
        
        if([openSet isSubsetOfSet:allowedSet])
                NSLog(@"The user has only the okay applications open");
        else
                NSLog(@"The user has non-allowed applications open");

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to