Why are these variables not being initialized?

Discussion in 'iOS Programming' started by ataylor2009, Mar 20, 2010.

  1. ataylor2009 macrumors member

    Joined:
    Jan 27, 2009
    #1
    Code:
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    		// create an array of rules
    		NSArray *rules = [NSArray arrayWithArray:[selectedContact objectForKey:kRULES]];
    		NSMutableArray *phoneTypes = [[NSMutableArray alloc] init];
    		NSInteger i, ruleCount = [rules count];
    		for (i=0; i<ruleCount; i++) {
    			[phoneTypes addObject:[[rules objectAtIndex:i] objectForKey:kPHONETYPE]];
    		}
    	
    		// count the total # of unique phoneTypes
    	NSInteger mobile,home,work,homeFax,workFax,pager,assistant,car,companyMain,radio,iPhone,other = 0;
    	NSString *tempType;
    	for (i=0; i<[phoneTypes count]; i++) {
    		tempType = [phoneTypes objectAtIndex:i];
    		if (tempType == @"mobile")
    			mobile++;
    		else if (tempType == @"home")
    			home++;
    		else if (tempType == @"work")
    			work++;
    		else if (tempType == @"home fax")
    			homeFax++;
    		else if (tempType == @"work fax")
    			workFax++;
    		else if (tempType == @"pager")
    			pager++;
    		else if (tempType == @"assistant")
    			assistant++;
    		else if (tempType == @"car")
    			car++;
    		else if (tempType == @"company main")
    			companyMain++;
    		else if (tempType == @"radio")
    			radio++;
    		else if (tempType == @"iPhone")
    			iPhone++;
    		else if (tempType == @"other")
    			other++;
    		}
    	
    		// delete duplicate phoneType objects so only one of each remains
    		// number of sections = array.count
    		return 2; // this is temporary; remove when above code works
    }
    I am trying to count the individual number of occurrences of each string shown in the if-else if statements above. My end-goal is to have a grouped table view, where the section headers are the phone types (i.e., "iPhone") and the number of rows in the section is the number of occurrences of "iPhone" in the array. When the above code works, I'll write a new bit that removes the duplicate entries so I'm left with a small array from which to pull the section headers - but, I need the count of items first, so I know how many rows are in each section.

    My problem is, the code above (specifically, the second part, where I'm trying to count the occurrences) doesn't work. For some reason, the integer variables are not getting set to zero. When I set a breakpoint at the last statement ("return 2;") and examine the variable values, they're all weird, impossible values - like 3567893 or something.

    I've tried changing the type from NSInteger to int, I've tried individually setting each variable to zero (instead of the way it's shown above), and I've inspected the variable values before the for loop starts. Clearly I am doing something wrong, but I have no idea what it is.

    Any help would be greatly appreciated. Thanks in advance.
     
  2. bredell macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #2
    I can see two problems. First, if you declare and initialize variables like this:

    Code:
    NSInteger Foo,Bar,Baz = 0;
    only the last variable gets initialized, the other variables contain random values. You could try this:

    Code:
    NSInteger Foo = Bar = Baz = 0;
    if you want to initialize them all.

    You also can't compare text strings the way you do. The way you do it, you're actually comparing two text objects, not the contents of the string in the objects. You should use this:

    Code:
    else if ([tempType isEqualToString:@"home"])
     
  3. ataylor2009 thread starter macrumors member

    Joined:
    Jan 27, 2009
    #3
    Awesome. Works like a charm after making those two changes. Thanks for the pointers.
     

Share This Page