-3

I am learning how to write iOS apps and am learning Objective-C. I'm trying to take a form that has 7 fields and POST it to my webserver. I already have the code that does the POSTing, I just need to concatenate all the text input fields into one NSString

The text input fields are named in the .h file as follows:

ctName
ctAddress1
ctAddress2
ctCity
ctState
ctZIP ctPhone

Any help would be greatly apprectiated.

UPDATE

So I've figured it out, but I can't answer my own question for a couple of hours... Here's what I came up with. It may not be the most efficient way of doing it, but I've tested it and it works.

NSString *body     = [NSString stringWithFormat:@"stripeToken=%@", token.tokenId];
body = [body stringByAppendingString:@"&ctname="];
body = [body stringByAppendingString:self.ctName.text];
body = [body stringByAppendingString:@"&ctadd1="];
body = [body stringByAppendingString:self.ctAddress1.text];
body = [body stringByAppendingString:@"&ctadd2="];
body = [body stringByAppendingString:self.ctAddress2.text];
body = [body stringByAppendingString:@"&ctcity="];
body = [body stringByAppendingString:self.ctCity.text];
body = [body stringByAppendingString:@"&ctstate="];
body = [body stringByAppendingString:self.ctState.text];
body = [body stringByAppendingString:@"&ctzip="];
body = [body stringByAppendingString:self.ctZIP.text];
body = [body stringByAppendingString:@"&ctphone="];
body = [body stringByAppendingString:self.ctPhone.text];
  • 1
    Since the question is on concatenation look at: http://stackoverflow.com/questions/8853865/simple-string-concatenation-in-objective-c – James Black Feb 16 '14 at 01:22

1 Answers1

-1

Try this

NSString completeString = [NSString stringWithFormat:@"%@_%@_%@_%@_%@_%@_%@",_ctName,_ctAddress1,_ctAddress2,_ctCity,_ctState,_ctZIP,_ctPhone];

Notice all your variables have an underscore (_) as a prefix. For this to work, make sure you syntethize each and everyone of them in the .m like this

@syntethize ctName, ctAddress1, ...

And so on with the rest.

If you're working with storyboards, verify all variables are proper linked.

Mauricio Chirino
  • 1,182
  • 17
  • 19
  • I forgot: i added underscore in the stringWithFormat as a marker. That way, you can split it using (_) as a parameter and you'll have all your inidivual variables again. You can of course change that marker to the one of your preference – Mauricio Chirino Feb 16 '14 at 01:40