Search code examples
objective-cnsarrayuiswitch

Objective-C: NSArray of UISwitches


Ok so I;m building an interface to allow admins controlls over certain rules which are controlled using UISwitches (a lot of them)

so I've declared an array of switches

NSMutableArray *cameraSwitches = [[NSMutableArray alloc] initWithObjects:
loc0CamProfileSwitch, loc1CamProfileSwitch, loc2CamProfileSwitch,
loc3CamProfileSwitch, loc4CamProfileSwitch, loc5CamProfileSwitch,
loc6CamProfileSwitch, loc7CamProfileSwitch, loc8CamProfileSwitch, nil]; 

where each object inside the Array is a UISwitch,

and then I'm looping through a diff array checking for initial values of these switches

for (NSUInteger i = 0; i < [camP count]; i++) {

    if ([camP objectAtIndex:i] != 0) {

        //UISwitch *csw = [cameraSwitches objectAtIndex:i];
        //[csw setOn:YES];

        [[cameraSwitches objectAtIndex:i] setOn:YES];
    else { [loc7CamProfileSwitch setOn:NO]; }
}

But everything I'm trying is giving me an error at runtime, throwing an exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

using break points: I realize theres something wrong with this line

[[cameraSwitches objectAtIndex:i] setOn:YES]; - or with how i initiated the array any help is very very appreciated


Solution

  • cameraSwitches is correctly instantiated. that's why you get an exception raised when you try to reach index 0.

    But, did you checked that loc0CamProfileSwitch (index 0) is non nil?