Credential match from Plist

亡梦爱人 提交于 2019-12-13 18:18:36

问题


I have a plist with Array title and item0,item1,item2 is dictionary to store title and password of my app.now i want to check for signin process

now i Want to check for pass and items should be matched when i press submit button to login.i Tried the below code but xcode crashes
-(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"); 
 } 
 }

and

-(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;

 }

the xcode gets crashs ..please check in -(void)authenticateCredentials where i am incorrect?

AND Crash when I select the submit button,Its wont give any output correct or incorrect in nslog and Xcode crash then , with errorr

 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

and two days ago this same code worked fine

and if i put Breakpoint for IF condition and later if fails just after if condition.


回答1:


Modify your authenticateCredentials as shown below and check if it is working. If it is showing an error message as Error! Not a dictionary, you need to check if your plist is having correct structure. Mostly your [objDict objectForKey:@"pass"] is returning a different data type.

- (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");
        }
    }
}


来源:https://stackoverflow.com/questions/13873968/credential-match-from-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!