如何从Label中的pList获取数据 [英] How to fetch data from pList in Label
问题描述
我有 RegistrationController
屏幕存储 email-id
,密码
, DOB
,高度
,重量
和 logininController
用于匹配 email-id
和密码
的屏幕以进行记录 - 目的。
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.
现在,在某些第三个
屏幕中,我只需要获取登录用户的plist中的高度
,权重
将其显示在label.now上如果我存储<$ c $的值c> email-id 和密码
进入 LoginViewController
in string并在其中调用如果匹配则匹配的新屏幕然后给出高度
,权重
..如果它更正了如何获取高度
,权重
来自同一个人的plist。
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.
如何从字符串中存储的plist中获取?
How can I fetch from the stored plist in a string?
这是我的代码:
-(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");
}
}
}
推荐答案
首先从你的plist文件中获取整个值,然后将 NSArray
存储到 NSMutableArray
中并获取值它的 objectAtIndex
和 valueForKey
property..see完整的例子如下..
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..
更新:
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...
这篇关于如何从Label中的pList获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!