你比较UITextFiled和NSString

时间:2011-08-10 09:10:30

标签: objective-c

请为什么我无法比较我的文本字段值?

   -(void)testPass:(id)sender
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Info.plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

    NSString *value;
    value = [dict valueForKey:@"Password"];





    if ([value isEqualToString:password.text]) {

        res = [[XMLTestViewController alloc] initWithNibName:@"XMLTestViewController" bundle:nil];
        res.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self.view addSubview:res.view];
    }
    else {


        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"IAA" 
                                                        message:@"Wrong Password" delegate:self cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"OK", nil];


        [alert show];
        [alert release];
    }

}

2 个答案:

答案 0 :(得分:0)

首先,您应该使用NSDictionary的{​​{1}}代替KVC的objectForKey:来访问字典内容。

其次,您确定valueForKey:返回正确的值吗?

答案 1 :(得分:0)

-(void)testPass:(id)sender
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Info.plist"];
        NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

        NSString *value;
        value = [dict valueForKey:@"Password"];

    //to test the values in console
    NSLog(@"Value; %@", value);
    NSLog(@"PWordText: %@", password.text);



        if ([value isEqualToString:password.text]) {

            res = [[XMLTestViewController alloc] initWithNibName:@"XMLTestViewController" bundle:nil];
            res.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
            [self.view addSubview:res.view];
        }
        else {


            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"IAA" 
                                                            message:@"Wrong Password" delegate:self cancelButtonTitle:@"Cancel"
                                                  otherButtonTitles:@"OK", nil];


            [alert show];
            [alert release];
        }

}

在此之后检查控制台是否在那里打印了值。