-4

I have a function that checks to see if the UISwitch in the parameter is switched on, if it is, then it will return true. I have an if statement that checks if the function returns true, if it is in fact true, it should nslog, but it doesnt. Any ideas on why when I press the switch it doesnt run? Here's what i'm doing:

-(BOOL)switched:(UISwitch *)currentSwitch{
    return NO;
    if([currentSwitch isOn]){
        return YES;
    }
}

viewWillDisappear{
    if([self switched:campSwitch] == YES){
        NSLog(@"THE FUNCTION RETURNED TRUE");
    }
}
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • 6
    uhh move the return NO statement below the conditional? – Patrick Goley Oct 11 '13 at 00:54
  • 2
    [Don't compare to `YES`](http://blog.bignerdranch.com/564-bools-sharp-corners/) – Sebastian Oct 11 '13 at 00:57
  • @PatrickGoley: This was the problem. Sorry, this was the first BOOL method I ever made. I thought it ran through the whole block. – Josue Espinosa Oct 11 '13 at 00:58
  • 3
    Also why not just use `if (campSwitch.on)`? – Sebastian Oct 11 '13 at 00:58
  • 1
    Don't feel too bad about that one. I remember a guy winning a $1000 bounty (back when that was a lot of money) for finding a "show-stopper" bug that turned out to be a return at the entry of the method. – Hot Licks Oct 11 '13 at 01:01
  • The `viewWillDisappear` method is syntactically incorrect as it is above. Also, if you are intending to override the `UIViewController` method the correct signature is `viewWillDisappear:`. And it should call the `super` method in its implementation. – ThomasW Oct 11 '13 at 01:05
  • @ThomasW: I was just typing it that way because I didn't actually want to type out the entire method name. I am aware that that isn't how you call it. Thank you for noticing though. – Josue Espinosa Oct 11 '13 at 01:06
  • +1 for actually copying the exact code of the problem method. – Hot Licks Oct 11 '13 at 01:10
  • 4
    This question should be closed now that it is solved because it is predicated on a typo and will not be useful to future readers. – jscs Oct 11 '13 at 01:11
  • _No_ function or method continues after a return statement is executed. The return type has nothing to do with it. How could the procedure possibly both stop and pass back a value _and also_ continue? – jscs Oct 11 '13 at 01:14
  • @JoshCaswell: I didn't know, but now I do. Thanks! – Josue Espinosa Oct 11 '13 at 01:14

2 Answers2

4

Change your code to the following:

-(BOOL)switched:(UISwitch *)currentSwitch{
    return [currentSwitch isOn];
}
Stunner
  • 12,025
  • 12
  • 86
  • 145
  • 3
    A function to return a property? While technically correct for his question, stepping back I would think the answer is that function isn't useful at all. – Joel Oct 11 '13 at 01:07
  • @Joel: I am going to use the function for more than one switch. Clearly if I wasn't, there would be no point to even creating it. – Josue Espinosa Oct 11 '13 at 01:08
  • @Josue each switch has that property. The fact that you need to access that property for multiple switch doesn't mean you need a function to do it. It's still pointless. – Joel Oct 11 '13 at 01:09
  • Exactly, you replaced one line with three (including definition and braces) so you didn't save yourself anything – borrrden Oct 11 '13 at 01:10
0
-(BOOL)switched:(UISwitch *)currentSwitch{
    return [currentSwitch isOn];
}

-(void)viewWillDisappear:(BOOL)animated{
    if([self switched:mySwitch]){
        [self saveToCoreData];
        NSLog(@"mySwitch was on.");
    }
}
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • Why would you call this one line function at all? Just put if ([mySwitch isOn]) {} – Joel Oct 11 '13 at 01:05
  • @Joel: because I am going to use that function for multiple switches, I was just testing with one to make sure it ran properly. – Josue Espinosa Oct 11 '13 at 01:07
  • That doesn't change the point of my comment. You are calling a function to return a property. It's pointless. – Joel Oct 11 '13 at 01:08
  • I'm not making the function to be useful, I am learning objective-c and was trying out BOOL functions, that is why I have a simple error above. – Josue Espinosa Oct 11 '13 at 01:10