Plist的凭证匹配

时间:2012-12-14 06:46:50

标签: iphone ios xcode plist

我有plist Array标题,item0item1item2是存储我app的标题和密码的字典。我想要检查登录过程

eneter here 现在我想检查通行证,当我按下提交按钮登录时应该匹配项目。我试过下面的代码,但xcode崩溃

-(void)authenticateCredentials 
{ 
NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[self readFromPlist]]; 

for (int i = 0; i< [plistArray count]; i++) 
{ 
 if ([[[plistArray   objectAtIndex:i]objectForKey:@"pass"]isEqualToString:emailTextFeild.text] && [[[plistArray objectAtIndex:i]objectForKey:@"title"]isEqualToString:passwordTextFeild.text]) 
{ 
NSLog(@"Correct credentials"); 
 return;
} 
   NSLog(@"INCorrect credentials"); 
 } 
 }

-(NSArray*)readFromPlist
 {
   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  NSUserDomainMask, YES); 
  NSString *documentsDirectory = [documentPaths objectAtIndex:0];
 NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"XYZ.plist"];

   NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

   NSArray *valueArray = [dict objectForKey:@"title"];

   return valueArray;

 }

xcode崩溃..请检查-(void)authenticateCredentials我不正确的地方?

当我选择submit按钮时崩溃,它不会在nslog中给出任何输出正确或不正确,然后Xcode崩溃,带有错误

 2012-12-14 12:10:45.142 NavTutorial[1661:f803] emailEntry: hi@gmail.com.com
2012-12-14 12:10:45.145 NavTutorial[1661:f803] passwordEntry: hellohello
2012-12-14 12:10:45.153 NavTutorial[1661:f803] -[__NSCFConstantString objectForKey:]:   unrecognized selector sent to instance 0x1568cd8
2012-12-14 12:10:45.155 NavTutorial[1661:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]:     unrecognized selector sent to instance 0x1568cd8'
 *** First throw call stack:
(0x14d7052 0x11c2d0a 0x14d8ced 0x143df00 0x143dce2 0x3981 0x14d8ec9 0x3735c2 0x37355a 0x418b76 0x41903f 0x417e22 0x39893f 0x398c56 0x37f384 0x372aa9 0x1bcdfa9 0x14ab1c5 0x1410022 0x140e90a 0x140ddb4 0x140dccb 0x1bcc879 0x1bcc93e 0x370a9b 0x211d 0x2095 0x1)
terminate called throwing an exception

两天前这个相同的代码工作正常

如果我将断点置于IF条件,稍后如果在条件之后就失败。

1 个答案:

答案 0 :(得分:1)

修改您的authenticateCredentials,如下所示,并检查它是否有效。如果它显示错误消息为Error! Not a dictionary,则需要检查您的plist是否具有正确的结构。大多数情况下,[objDict objectForKey:@"pass"]会返回不同的数据类型。

- (void)authenticateCredentials {
    NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[self readFromPlist]];

    for (int i = 0; i< [plistArray count]; i++)
    {
        id object = [plistArray objectAtIndex:i];

        if ([object isKindOfClass:[NSDictionary class]]) {
            NSDictionary *objDict = (NSDictionary *)object;

            if ([[objDict objectForKey:@"pass"] isEqualToString:emailTextFeild.text] && [[objDict objectForKey:@"title"] isEqualToString:passwordTextFeild.text])
            {
                NSLog(@"Correct credentials");
                return;
            }
            NSLog(@"INCorrect credentials");
        } else {
             NSLog(@"Error! Not a dictionary");
        }
    }
}