NSDictionary编码兼容

时间:2015-07-24 03:40:02

标签: ios objective-c

我有这段代码,用于检查用户是否在用户名和密码文本字段中输入了内容,密码文本字段中的密码与确认密码文本字段中的密码匹配:

- (IBAction)createAccountButton:(id)sender {
    if([self.createPasswordTextField.text isEqualToString:self.confirmPasswordTextField.text] && self.createPasswordTextField.text.length != 0 && self.createUserNameTextField.text.length != 0){
        NSDictionary *userAccountDetails = [[NSDictionary alloc] init];
        [userAccountDetails setValue:self.createUserNameTextField.text forKey:@"username"];
        [userAccountDetails setValue:self.createPasswordTextField forKey:@"password"];
        [[NSUserDefaults standardUserDefaults] setObject:self.users forKey:@"users"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [self.users addObject:userAccountDetails];
    }else{
        UIAlertView *passwordMismatchAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Passwords do not match!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [passwordMismatchAlert show];
    }
}

由于某种原因,每次我点击createAccountButton时,我都会在Xcode中收到一条错误:这个类不是密钥值的密码值编码符号。我确保我的IB网点没有被打破,因为很多其他帖子指出了这一点。所以我不确定这种方法发生了什么。有什么见解吗?

2 个答案:

答案 0 :(得分:3)

您应该使用NSMutableDictionarysetObject:forKey:

 NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
[dic setObject:yourobj forKey:yourkey];

所以在你的代码中

 NSMutableDictionary *userAccountDetails = [[NSMutableDictionary alloc] init];
[userAccountDetails setObject:self.createUserNameTextField.text forKey:@"username"];
[userAccountDetails setObject:self.createPasswordTextField forKey:@"password"];
[[NSUserDefaults standardUserDefaults] setObject:self.users forKey:@"users"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self.users addObject:userAccountDetails];

答案 1 :(得分:0)