Ios 使用NSMutableDictionary进行验证

Ios 使用NSMutableDictionary进行验证,ios,objective-c,xcode,nsmutablearray,Ios,Objective C,Xcode,Nsmutablearray,我有一个结合了用户名和密码的NSMutableDictionary,如何使用objective C验证它 例如: 如何将用户名和密码作为键值对进行验证。多种方法: 一行: NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"username1", @"pass1", @"username2", @"pass2", @"username3", @"pass3",

我有一个结合了用户名和密码的
NSMutableDictionary
,如何使用objective C验证它

例如:

如何将用户名和密码作为键值对进行验证。

多种方法:

一行:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
         @"username1", @"pass1",
         @"username2", @"pass2",
         @"username3", @"pass3",
         @"username4", @"pass4", nil];
另一种方式:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"username1" forKey:@"pass1"];
[dict setObject:@"username2" forKey:@"pass2"];
// so on ...
另一个使用
NSArray

NSArray *username = @[@"username1", @"username2", @"username3", @"username4"];
NSArray *passwords = @[@"pass1", @"pass2", @"pass3", @"pass4"];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:username forKeys:passwords];

// see output
NSLog(@"%@", dict);
// separately
NSLog(@"Usernames: %@", [dict allValues]);
NSLog(@"Passwords: %@", [dict allKeys]);
您可以通过相应地提取单独的键和值或使用块enumerateKeysAndObjectsUsingBlock进行验证:

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

    // place your validation code here 
    NSLog(@"There are %@ %@'s in stock", obj, key);
}];

.

要轻松验证字典的内容,只需访问键并验证值即可

例子:
//假设密钥是用户名,值是密码
NSDictionary*credential=@{“username1”:@“pass1”:@“username2”:@“pass2”/*,…等等*/};
NSString*用户名=@;
NSString*passwordInput=@;
NSString*密码=凭证[用户名];
//如果由于用户名不存在而导致密码为零,则以下条件将失败。
if([密码IsequalString:passwordInput]){
//密码和用户名都匹配
}
否则{
//用户名或密码不匹配
}

这里的关键是用户名?@krishna Skw首先你告诉我你必须进行哪种用户名验证?意思是像电子邮件验证?克里希纳检查我的ans,让我知道你的反馈。
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

    // place your validation code here 
    NSLog(@"There are %@ %@'s in stock", obj, key);
}];
// this assumes that the key is the username and the value is the password
NSDictionary *credential = @{@"username1":@"pass1",@"username2":@"pass2"/* , ..and so on */};

NSString *username = @"<user_input_or_whatever>";

NSString *passwordInput = @"<user_input_or_whatever>";

NSString *password = credential[username];

// if password is nil because username is not present the the condition below fails.
if([password isEqualToString:passwordInput]){
   // both password and username matched
}
else{
  // username or password didn't matched
}