问题
I have a RegistrationController
screen to store email-id
,password
,DOB
,Height
,Weight
and logininController
screen to match email-id
and password
to log-in purpose.
Now, In some third
screen I have to fetch only the Height
,Weight
from the plist of the logged-in user to display it on the label.now if I Store the values of email-id
and password
in from LoginViewController
in string and call it in the new screen to match if matches then gives Height
,Weight
..if it corrects then how to fetch Height
,Weight
from the plist of the same one.
How can I fetch from the stored plist in a string?
Here is my code:
-(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;
}
- (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");
}
}
}
回答1:
First get whole value from your plist file after that store this NSArray
into NSMutableArray
and get the value with its objectAtIndex
and valueForKey
property..see whole example bellow..
UPDATE :
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"plist"];
NSArray *contentArray = [NSArray arrayWithContentsOfFile:plistPath];
NSMutableArray *yourArray = [[NSMutableArray alloc] initWithArray:contentArray];
for (int i = 0; i< [yourArray count]; i++)
{
id object = [yourArray objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]]) {
NSDictionary *objDict = (NSDictionary *)object;
yourLableWeight.text = [[objDict objectForKey:@"Weight"];// set index with your requirement
yourLableHeight.text = [[objDict objectForKey:@"Height"];
}
hope this help you...
回答2:
When you enter credentials on login screen to check when it match the credentials with the fetched plist then pass that plist to the next controller. Do something like this
UserViewController *controller = [[UserViewController alloc] initWithNibName:@"UserViewController" bundel:nil];
[controller setUserDictionary:yourPlistDictionary];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
in UserViewController you would have a NSDictionary instance to store the data to show, hope that will help you
来源:https://stackoverflow.com/questions/13950903/how-to-fetch-data-from-plist-in-label