2 bluetooth99 bluetooth99 于 2013.06.05 10:16 提问

通过名称对数组进行初始化

要初始化数组中的字符串,如下格式:

-(IBAction)btnSubmit:(UIButton *)sender
{
NSString *phone = phoneNumber.text;
NSLog(@"Phone  :%@",phone);
NSString *TxtUserName = userName.text;
NSString *TxtEmailId = emailId.text;
NSArray *details = [NSArray arrayWithObjects:TxtUserName,TxtEmailId,phone,nil]; 
}

得到的内容:( "xxxxxxx", "xxxxx@gmail.com", 7675 )

但是期望的内容:("Name=xxxx","Email=xxxx@gmail.com","Phone=8786")

请高手指教,谢谢。

2个回答

xiaoyaokk
xiaoyaokk   2013.06.05 11:30
已采纳

修改代码如下:

NSString *phone = [NSString stringWithFormat:@"Phone=%@",phoneNumber.text];
NSString *TxtUserName = [NSString stringWithFormat:@"Name=%@",userName.text];
NSString *TxtEmailId = [NSString stringWithFormat:@"Email=%@",emailId.text];
NSArray *details = [NSArray arrayWithObjects:TxtUserName,TxtEmailId,phone,nil];

其实可以考虑放到字典中

zhanglei5415
zhanglei5415   Rxr 2013.06.05 11:12

你存的时候

NSArray *details = [NSArray arrayWithObjects:TxtUserName,TxtEmailId,phone,nil];

输出肯定是 ( "xxxxxxx", "xxxxx@gmail.com", 7675 )

如果想得到你要的效果的话,需要自己把字符串拼接起来了

NSArray *details = [NSArray arrayWithObjects:[NSString  stringWithFormat:@"Name=%@",TxtUserName],
                                             [NSString stringWithFormat:@"Email=%@",TxtEmailId],
                                             [NSString stringWithFormat:@"Phone=%@",phone],nil];

其实你使用NSDictionary来存储更适合这种键值对的数据

NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:TxtUserName,@"Name",TxtEmailId,@"Email",phone,@"Phone",nil];
NSLog(@"result %@",dict);
Csdn user default icon
上传中...
上传图片
插入图片