Luke
Luke

Reputation: 612

Finding most common array within an NSObject

I have an NSObject "TeamStats" that holds an array of "Possession" objects. The "Possession" objects hold an array of "Players"... For every "Game" there is a "TeamStats" created as well. Two properties on the "Possession" object are didEndInTurnover and didEndInScore. What I need to do is find the most common array of players within a possession that didEndInScore and didEndInTurnover...

What is the best way to approach this?

Upvotes: 0

Views: 116

Answers (1)

GeneralMike
GeneralMike

Reputation: 3001

Okay, to start, I'd simplify this a little by replacing the array of players with a new NSString variable that just tells what squad that data is for (so you'll have a NSString *squadName variable (and you would set posession1.squadName = @"Squad A" or something similar) as a property of your Possession object, instead of the array of players. If you want to print out player names or numbers, you could always set up an array or dictionary somewhere else that holds that information for printing to screen - something like

NSArray squad1Array = [NSArray arrayWithObjects:@"Bill",@"Tom",@"Joe",nil];
NSArray squad2Array = [NSArray arrayWithObjects:@"Jim",@"Jeff",@"Mike",nil];

Once you have the squads set up, you'll need to loop through the array of Possession objects. In that loop, first check didEndInTurnover. Then you need integer variables for each squad and each didEndIn... (so something like int squadAScore, int squadBScore, int squadATurnover, int squadBTurnover and so forth). When you are looping through, check what squad was active for that possession, and increase the integer counter for that squad as appropriate. Lastly, just compare your integer counters once you have looped through all the possessions to see which one is greatest.

Note that if you wanted to organize this a little better or make it more flexible and less hardcoded, you could probably look into using nested NSDictionaries instead of the integer counters - it would take a bit more work to set up as dictionaries, but if you need to add a new squad later or something, it might be easier to make those kind of changes in the future.

Well, I don't know if any of that is understandable or not. If it doesn't really make sense, I could try to put some code of what the loop would roughly look like, just let me know.

EDIT: Here's some sample for what the loop is going to look like, using the dictionaries:

// Define counters for squads
NSMutableDictionary *turnoverDictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *scoreDictionary = [[NSMutableDictionary alloc] init];

// Loop over all possessions
for (YourPossessionClass *currentPossession in myTeamStats.possessionArry)
{
    if (currentPossession.didEndInTurnover == YES)
    { 
        BOOL found = NO;
        for (NSString *squad in [turnoverDictionary allKeys])
        {
            if ([squad isEqualToString:currentPossession.squad])
            {
                int count = [[turnoverDictionary objectForKey:squad] intValue];
                count ++;
                NSNumber *newCount = [NSNumber numberWithInteger:count];
                [turnoverDictionary setObject:newCount ForKey:squad];
                found = YES;
            }
            //else {Do Nothing - this isn't the squad we want}
        }

        // Add a new entry to the dictionary for this squad if this squad didn't exist before
        if (!found)
        {
            [turnoverDictionary setObject:[NSNumber numberWithInt:0] forKey:currentPossession.squad];
        }
        //else {Do Nothing - squad was already in dictionary}
    }
    else if {currentPossession.didEndInScore == YES}
    {
        // ... Do all the same stuff as we did for turnover, except now add them to the scoreDictionary instead of the turnoverDictionary ...
    }
}

Upvotes: 1

Related Questions