如何从iOS中添加NSString的价值观? [英] how to add values from nsstrings in iOS?

查看:274
本文介绍了如何从iOS中添加NSString的价值观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个按钮。每个按钮都有像一些价值 1,2,3,4,5 在名为字符串 totalSeats_selected 。结果
我想将它们保存到的NSString 的结果显示的是这样的:结果
  的NSString *结果= [1,2,3,4,5];

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];

这些值是作为请求Web服务,所以它必须是该格式发送的座位总数是多少?

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

那么,怎样才能全部的NSString 这些值保存按钮事件?

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

推荐答案

使用像this.Suppose您的字符串是整数意味着使用方法1。否则使用方法2(如果分配中的NSString你的价值)。

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);

}
 }

这篇关于如何从iOS中添加NSString的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆