-1

I have an NSArray of UISwitches. I have separately an NSDictionary whose keys are NSNumbers, and whose objects are BOOL values in the form of NSString objects. What I would like to do is iterate through the NSArray of UISwitches, check to see if the tag value is one of the keys inside the NSDictionary, and if a match is found, then set the enabled property of the UISwitch to the key's corresponding object (after converting it to a BOOL from an NSString).

My code is as follows:

for (int i=0; i<[self.switchCollection count]; i++) {
     UISwitch *mySwitch = (UISwitch *)[self.switchCollection objectAtIndex:i];
     if (tireSwitch.tag == //this has to match the key at index i) {
                    BOOL enabledValue = [[self.myDictionary objectForKey:[NSNumber numberWithInt://this is the key that is pulled from the line above]] boolValue];
                    mySwitch.enabled = enabledValue;
     }
 }
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • 3
    What is the problem you're experiencing? – jscs Jan 24 '14 at 19:46
  • I don't know how to get the key from the dictionary that corresponds to the index i inside my for loop. – syedfa Jan 24 '14 at 19:53
  • Huh? Your code already does that: `[self.myDictionary objectForKey:[NSNumber numberWithInt:i]]` – jscs Jan 24 '14 at 19:54
  • That retrieves the object that corresponds to the key. I need the key itself (which is different from the index i). I then need to use the key that I get, to then retrieve the correct object. – syedfa Jan 24 '14 at 19:57
  • The key is something other than `[NSNumber numberWithInt:i]`? What is it? How did you build the dictionary in the first place? – jscs Jan 24 '14 at 19:58
  • It is composed of two arrays. One for keys, which are NSNumbers, and the objects are NSStrings. – syedfa Jan 24 '14 at 19:58
  • 1
    Give us an example of what's inside the dictionary, what's inside the array, and what "connection" you want between them – Larme Jan 24 '14 at 20:00

2 Answers2

2

Now that Duncan C's answer has made clear what you're trying to accomplish, it can be written much more simply.

Iterate the array directly. You don't need i at all, since you're not using it to access anything other than the array.

For each switch, try to get a value from the dictionary using the tag (this is wrapped in an NSNumber using the @() boxing syntax.

If a value exists, then set the switch's enabled.

for( UISwitch * switch in self.switchCollection ){
    NSString * enabledVal = self.myDictionary[@(switch.tag)];
    if( enabledVal ){
        switch.enabled = [enabledVal boolValue];
    }
}
jscs
  • 63,694
  • 13
  • 151
  • 195
  • 1
    Josh, while your code is more compact, I think it's better to be verbose in forum posts, from a pedagogical point of view. – Duncan C Jan 24 '14 at 21:34
  • 1
    I'd rather describe in English what the idiomatic code does, @DuncanC, so that the goals of understanding and good practice are both served. Fortunately, we can both have it our own way! My answer was not intended as a criticism of yours. :) – jscs Jan 24 '14 at 21:38
  • 1
    @JoshCaswell, true enough. We're both trying to help. (+1) – Duncan C Jan 25 '14 at 16:45
  • Can either of you (Josh or Duncan) suggest a good book on advanced Objective-C programming? I honestly am impressed with the proficiency that both of you have demonstrated here. Thanks again for taking the time to provide a solution for me here. – syedfa Jan 28 '14 at 15:23
  • 1
    There aren't really "advanced ObjC books", @syedfa, I'm afraid. You've just got to do it. However, there's a few "recipes" books around, and one called (IIRC) "Cocoa Design Patterns". Big Nerd Ranch has an "Advanced OS X Programming" book (and all their stuff is excellent), but it's about system features, not ObjC. – jscs Feb 02 '14 at 07:13
1

Your code doesn't look right. How about this:

(Edited to use fast enumeration (for...in loop syntax)

//Loop through the array of switches.
for (UISwitch *mySwitch  in self.switchCollection) 
{
     //Get the tag for this switch
  int tag = mySwitch.tag;

  //Try to fetch a string from the dictionary using the tag as a key
  NSNumber *key = @(tag);
  NSString *dictionaryValue = self.myDictionary[key];

  //If there is an entry in the dictionary for this tag, set the switch value.
  if (dictionaryValue != nil) 
  {
    BOOL enabledValue = [dictionaryValue boolValue];
    mySwitch.enabled = enabledValue;
  }
}

That's assuming I understand what you're trying to do...

Duncan C
  • 128,072
  • 22
  • 173
  • 272