我如何正确格式化NSString?

时间:2013-12-19 07:52:49

标签: objective-c ios7 xcode5

好的,这是一个黄色的爆炸错误,上面写着“格式字符串未使用的数据参数”

中的.h:

@property (strong, nonatomic) IBOutlet UITextField *UserFirstName;

@property (strong, nonatomic) IBOutlet UITextField *UserLastName;

@property (strong, nonatomic) IBOutlet UITextField *UserEmail;

@property (strong, nonatomic) IBOutlet UITextField *UserUser;

@property (strong, nonatomic) IBOutlet UITextField *UserPassword;

@property (strong, nonatomic) IBOutlet UITextField *UserConfirmPassword;

@property (nonatomic,retain) NSString *userFirstNameEntry;
@property (nonatomic,retain) NSString *userLastNameEntry;
@property (nonatomic,retain) NSString *emailEntry;
@property (nonatomic,retain) NSString *userEntry;
@property (nonatomic,retain) NSString *userPasswordEntry;
@property (nonatomic,retain) NSString *userConfirmPasswordEntry;

- (IBAction)userCreate:(UIButton *)sender;

@end

在.m

- (IBAction)userCreate:(UIButton *)sender
{
    _userFirstNameEntry = UserFirstName.text;
    _userLastNameEntry = UserLastName.text;
    _emailEntry = UserEmail.text;
    _userEntry = UserUser.text;
    _userPasswordEntry = UserPassword.text;
    _userConfirmPasswordEntry = UserConfirmPassword.text;


NSString *postData = [NSString stringWithFormat:@"FirstName=%@&LastName=%@&Email=%@&User=%@&Pass=%@&ConfirmPass", _userFirstNameEntry, _userLastNameEntry, _emailEntry, _userEntry, _userPasswordEntry, _userConfirmPasswordEntry];

我相信错误是使用NSString postData stringWithFormat,但我找不到任何正确的源代码可以使我得到正确的格式,我想我的问题可能就是设置NSString的方法。

2 个答案:

答案 0 :(得分:3)

您的stringWithFormat:有5个占位符(%@),但您传递了6个对象...

答案 1 :(得分:1)

您忘记添加占位符以确认密码

NSString *postData = [NSString stringWithFormat:@"FirstName=%@&LastName=%@&Email=%@&User=%@&Pass=%@&ConfirmPass=%@", _userFirstNameEntry, _userLastNameEntry, _emailEntry, _userEntry, _userPasswordEntry, _userConfirmPasswordEntry];