how to add values from nsstrings in iOS?

2019-09-21 19:36发布

问题:

I have 5 buttons. Each button has some value like 1,2,3,4,5 in a string named totalSeats_selected.
I want to save them into a nsstringthat shows result like this:
nsstring * result = [1,2,3,4,5];

These values are to used as request for a web service, so it must be in this format to send total number of seats?

So, how to save all these values in nsstring on button event?

回答1:

Use like this.Suppose your string is in integer means use Method 1 .otherwise use Method 2(If your value assigned in NSString).

 Method 1:



- (IBAction)Savebtnpressed:(id)sender {

NSMutableDictionary *datadict=[NSMutableDictionary dictionary];


[datadict setValue:[NSNumber numberWithInt:[first intValue]] forKey:@"1"];

[datadict setValue:[NSNumber numberWithInt:[second intValue]] forKey:@"2"];

[datadict setValue:[NSNumber numberWithInt:[third intValue]] forKey:@"3"];

[datadict setValue:[NSNumber numberWithInt:[fourth intValue]] forKey:@"4"];

[datadict setValue:[NSNumber numberWithInt:[fifth intValue]] forKey:@"5"];

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);

 }
   }

   Method 2:


    - (IBAction)Savebtnpressed:(id)sender {

NSMutableDictionary *datadict=[NSMutableDictionary dictionary];

 [datadict setObject:[NSString stringWithFormat:@"%@",FIRST] forKey:@"1"];

[datadict setObject:[NSString stringWithFormat:@"%@",SECOND] forKey:@"2"];

   [datadict setObject:[NSString stringWithFormat:@"%@",THIRD] forKey:@"3"];

[datadict setObject:[NSString stringWithFormat:@"%@",FOURTH] forKey:@"4"];

 [datadict setObject:[NSString stringWithFormat:@"%@",FIFTH] forKey:@"5"];


NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  //    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);

}
 }