Reputation:
I am using several UISwitches and I can't get them to set correctly, for some odd reason.
I am pulling in JSON data which is just basically a bunch of integers in a certain sequence, based off of their values, I want a switch to be set to either on, or off. Here is part of the relevant code:
- (void)fetchData
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://10.101.10.101/get/channels"]];
NSError* error;
LightingData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
idData = [LightingData valueForKey:@"level"];
NSLog(@"Lighting Values: %@", [idData objectAtIndex:19]);
dispatch_async(dispatch_get_main_queue(), ^{
});
});
}
- (void)dataCheck
{
int x = 100;
if ([idData objectAtIndex:19 == x])
{
[fieldsOverheadStatus setOn:YES];
}
}
The dataCheck
method continues for a while, setting about 20 switches.
I have all of my switches set to default as "off", but when I run the app, they all show as being "on".
I set breakpoints to see if the dataCheck
method gets called, and it does. As you can see, I stuck a log in to see what the value of objectAtIndex:19
is and the value that is returned is 0 (not null). I checked the contents of idData
and all of the values are correctly gotten.
Can anyone tell me why my switches are being changed, even when they shouldn't be? What am I doing wrong?
Let me know if anymore code is needed.
Upvotes: 0
Views: 124
Reputation: 57149
You’re checking the object in idData
at the index (19 == x)
, i.e. false
/NO
, i.e. 0; the object at index 0 is non-nil, so you’re always setting the switch on. That line should probably be:
if ([[idData objectAtIndex:19] intValue] == x)
Upvotes: 1