-3

I'm writing a bit of code which will write a text message within my app as shown below:

MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
[messageComposer setMessageComposeDelegate:self];
//    Check The Device Can Send Text Messages
if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]];
    [messageComposer setBody:messageBodyText];
    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
//        Need to add an alert view
    NSLog(@"TEXT ISNT WORKING");
}

}

So I currently have an if statement checking that the device is capable of sending messages, but how can I add ANOTHER if statement within that? I basically want to decide what the message body is depending on a switch position within my view like:

If switch is left : Message Body is A
If Switch is Right: Message Body is B

halfer
  • 19,824
  • 17
  • 99
  • 186
Gary Stewart
  • 161
  • 2
  • 11

2 Answers2

1

This really is a prime-example for a switch, that's even how Gary describes it.:

if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]

    //yourSwitchIsRightSide should be bool value
    switch (yourRightSide) {
        case YES:
            [messageComposer setBody:yourRightMessageBodyText];
            break;
        case NO:
            [messageComposer setBody:yourLeftMessageBodyText];
            break;
    }

    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
    //        Need to add an alert view
}

In addition to improving readability switch/case also scales a lot better. If Gary decides he would like to have a couple of additional options later on if-else would create a real mess of things. (The BOOL should probably be replaced by a check against an enum in this scenario)

switch (switchDirection) {
    case MFSwitchDirectionLeft:
        [messageComposer setBody:yourLeftMessageBodyText];
        break;
    case MFSwitchDirectionRight:
        [messageComposer setBody:yourRightMessageBodyText];
        break;
    case MFSwitchDirectionUp:
        [messageComposer setBody:yourUpMessageBodyText];
        break;
    case MFSwitchDirectionDown:
        [messageComposer setBody:yourDownMessageBodyText];
        break;
    default:
        break;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
  • Yes, i also same think about switch case. According to questioner scenario their are just a one condition so it not tolerate and increase the complexity of program. If he ask more than one condition then yes i definitely wrote my answer in switch case. – Tirth Nov 04 '13 at 16:11
  • Well I don't actually know that this is the cause of your down vote. As there was already an outer if-else in place I would PERSONALLY jump through hoops to avoid another one nested inside. I still think it's a bit harsh down-voting your answer, particularly as it was accepted and as you state is not all that complex... – T. Benjamin Larsen Nov 04 '13 at 17:00
0

Try given code. Check your switch value is in left or right, I'm consider your switch varible is yourSwitchIsRightSide.

MFMessageComposeViewController *messageComposer = [[MFMessageComposeViewController alloc]  init];
[messageComposer setMessageComposeDelegate:self];
//    Check The Device Can Send Text Messages
if ([MFMessageComposeViewController canSendText]) {
    [messageComposer setRecipients:[NSArray arrayWithObjects: nil]]

  //yourSwitchIsRightSide should be bool value
    if(yourSwitchIsRightSide){
        [messageComposer setBody:yourRightMessageBodyText];
    }
    else{
        [messageComposer setBody:yourLeftMessageBodyText];
    }

    [self presentViewController:messageComposer animated:YES completion:NULL];
} else {
//        Need to add an alert view
    NSLog(@"TEXT ISNT WORKING");
}
}
Tirth
  • 7,801
  • 9
  • 55
  • 88