遍历NSMutableDictionary NSMutableArray
//保存用户名和密码 -(void)saveAccountOrPwd { NSMutableDictionary *recorder = [NSMutableDictionary dictionary]; [recorder setObject:loginName.text forKey:LOGIN_NAME]; NSMutableArray *array = [[NSMutableArray alloc]init]; //array = [NSMutableArray arrayWithContentsOfFile:LOGIN_RECORDEDFILE]; if (recodePwdBtn.selected) { [recorder setObject:loginPWD.text forKey:LOGIN_PWD]; }else { [recorder setObject:@"" forKey:LOGIN_PWD]; } for(NSMutableDictionary *dic in array) { NSLog(@"dicdicdicdic:%@",dic); NSEnumerator * enumerator = [dic keyEnumerator]; id object; _userInfileOrNot = 0; while(object = [enumerator nextObject]) { NSLog(@"键值为:%@",object); //在这里我们得到的是键值,可以通过(1)得到,也可以通过这里得到的键值来得到它对应的value值 //通过NSDictionary对象的objectForKey方法来得到 //其实这里定义objectValue这个对象可以直接用NSObject,因为我们已经知道它的类型了,id在不知道类型的情况下使用 id objectValue = [dic objectForKey:object]; if(objectValue != nil) { NSLog(@"loginname:%@value:%@",loginName.text,objectValue); if([loginName.text isEqualToString: objectValue]) { //如果遇到有相同的用户名。计数器+1 _userInfileOrNot = _userInfileOrNot+1; NSLog(@"inininini"); } else { //_userInfileOrNot = NO; } NSLog(@"%@所对应的value是 %@",object,objectValue); } } } //如果当前用户名在文件中不存在。则保存.当计数器为0时。表示文件中不存在。则保存 if(!_userInfileOrNot) { NSLog(@"saveinfo:%d",_userInfileOrNot); [array addObject:recorder]; } [array writeToFile:LOGIN_RECORDEDFILE atomically:YES ]; //[recorder writeToFile:LOGIN_RECORDEDFILE atomically:YES ]; }
//遍历NSMutableDictionary for(NSDictionary *myDictionary in array) { NSArray *keys = [myDictionary allKeys]; for (NSString *_key in keys) { NSString *value = [myDictionary objectForKey:_key]; } } recorder = [NSMutableDictionary dictionaryWithContentsOfFile:LOGIN_RECORDEDFILE]; NSEnumerator * enumerator = [recorder keyEnumerator]; id object; _userInfileOrNot = NO; while(object = [enumerator nextObject]) { NSLog(@"键值为:%@",object); //在这里我们得到的是键值,可以通过(1)得到,也可以通过这里得到的键值来得到它对应的value值 //通过NSDictionary对象的objectForKey方法来得到 //其实这里定义objectValue这个对象可以直接用NSObject,因为我们已经知道它的类型了,id在不知道类型的情况下使用 id objectValue = [recorder objectForKey:object]; if(objectValue != nil) { if(objectValue==loginName.text) { _userInfileOrNot = YES; NSLog(@"inininini"); } else { _userInfileOrNot = NO; } NSLog(@"%@所对应的value是 %@",object,objectValue); } }