我有一个 RegistrationController
屏幕来存储 email-id
、password
、DOB
、Height
、Weight
和 logininController
屏幕,用于匹配 email-id
和 password
以达到登录目的。
现在,在某些第三
屏幕中,我必须从登录用户的plist中仅获取高度
、体重
来显示它在标签上。现在,如果我将 LoginViewController
中的 email-id
和 password
的值存储在字符串中,并在新屏幕中调用它匹配如果匹配则给出 Height
,Weight
..如果正确则如何获取 Height
,Weight
同一个的plist。
如何从字符串中存储的 plist 中获取?
这是我的代码:
-(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
属性..请参阅下面的整个示例..
更新:
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"];
}
希望这对您有帮助...
关于iphone - 如何从Label中的pList中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950903/