方法很多:
在一个行:
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);
}];
Complete source。
这里关键是用户名? –
@krishna Skw首先你说过我需要做哪种用户名验证?就像电子邮件验证? –
克里希纳检查我的答案,让我知道您的反馈。 – vaibhav