题目-字典
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @property (nonatomic, copy) NSString *userName; @property (nonatomic, copy) NSString *password; @property (nonatomic, copy) NSString *phone; @property (nonatomic) NSInteger age; //... @end
Person.m
#import "Person.h" @implementation Person @end
main.m
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *dic = @{@"abc":@"123", @"gcd":@"456"}; NSLog(@"%@", dic); NSString *userName = @"abc"; NSString *passowrd = @"123"; if ([dic objectForKey:userName] == nil) { NSLog(@"用户名不存在"); } else { //判断密码是否正确 if ([passowrd isEqualToString:[dic objectForKey:userName]]) { NSLog(@"登陆成功"); } else { NSLog(@"密码错误"); } } //2.... Person *p1 = [[Person alloc] init]; p1.userName = @"abc"; p1.password = @"123"; p1.phone = @"1111111"; Person *p2 = [[Person alloc] init]; p2.userName = @"gcd"; p2.password = @"123"; p2.phone = @"2222222"; NSDictionary *userDic = @{p1.userName:p1, p2.userName:p2}; NSLog(@"%@", userDic); //先把对象取出来 Person *p = [userDic objectForKey:userName]; if (p == nil) { NSLog(@"用户名不存在"); } else { //判断密码是否正确 if ([passowrd isEqualToString:p.password]) { NSLog(@"登陆成功"); NSLog(@"%@", p.phone); } else { NSLog(@"密码错误"); } } } return 0; }