【问题标题】:how to add values from nsstrings in iOS?如何在 iOS 中从 nsstrings 添加值?
【发布时间】:2014-08-25 12:13:41
【问题描述】:

我有 5 个按钮。每个按钮在名为 totalSeats_selected 的字符串中都有一些值,例如 1,2,3,4,5
我想将它们保存到 nsstring 中,结果如下所示:
nsstring * result = [1,2,3,4,5];

这些值是用来作为对网络服务的请求,所以它必须是这种格式来发送席位总数?

那么,如何在按钮事件中将所有这些值保存在nsstring 中?

【问题讨论】:

  • 查看stringWithFormat:的文档。

标签: button ios7 nsstring


【解决方案1】:

像这样使用。假设你的字符串是整数意味着使用方法 1。否则使用方法 2(如果你的值在 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);

}
 }

【讨论】:

  • Ganesh ,但我需要另一个类中的 Web 服务 我只知道如何将这些整数保存在 nsstring 中,就像 [1,2,3] 一样,当我点击按钮时,它会在 nsstring 中添加 1 [1] 并且当我点击按钮 2 时,nsstring 必须变成 [1,2]...所以这是可能的
  • 并且在它的响应中它还显示在 2 个按钮上,点击如下:["1","2"] 但我需要像 [1,2] 一样打印它
  • 这样试试 -(IBAction)myButtonAction:(id)sender { NSLog(@"Button Tag is : %i",[sender tag]); switch ([sender tag]) { case 0: // Do some think here break; case 1: // Do some think here break; default: NSLog(@"这里的默认消息");休息;并让您的发件人使用新字符串,这可能是可能的。
猜你喜欢
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多