2017-02-03 9 views
-4

私はNSMutableDictionaryusername & passwordの組み合わせを持っていますが、どうすれば目的Cを使って検証できますか?例についてはNSMutableDictionaryを使用した検証

私は、キーと値のペアとしてユーザ名&パスワードの入力を検証することができますどのように

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init]; 

[dictionary setObject:@"A" forKey:@"A"]; 
[dictionary setObject:@"B" forKey:@"B"]; 
[dictionary setObject:@"C" forKey:@"C"]; 

+2

ここで重要なのは、ユーザー名ですか? –

+0

@krishna Skwの最初のすべてのあなたは、あなたがしなければならない種類のユーザー名検証を教えてくれましたか?電子メールの検証のような意味ですか? –

+0

クリシュナは私のansをチェックして、あなたのフィードバックを教えてください。 – vaibhav

答えて

0

多くの方法:1行で

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]); 
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 

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

Complete source:あなたはそれに応じて別のキーと値を抽出またはブロックenumerateKeysAndObjectsUsingBlockを使用して検証することができます。

0

辞書の内容を簡単に検証するには、キーにアクセスして値を検証するだけです。

例:

// 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 
} 
関連する問題