iphone - 如何从Label中的pList中获取数据

标签 iphone ios ios5 plist

我有一个 RegistrationController 屏幕来存储 email-idpasswordDOBHeight WeightlogininController 屏幕,用于匹配 email-idpassword 以达到登录目的。

现在,在某些第三屏幕中,我必须从登录用户的plist中仅获取高度体重来显示它在标签上。现在,如果我将 LoginViewController 中的 email-idpassword 的值存储在字符串中,并在新屏幕中调用它匹配如果匹配则给出 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/

相关文章:

ios - 在 iOS/Xcode 中将单个 View 转换为选项卡式应用程序

ios - 是否有关于何时使用模态转场以及何时在 iOS 中使用推送转场的一般经验法则?

iphone - 在 uiviewcontroller 中添加 uitableviewcontroller 作为 subview 时出错

iphone - 从具有多个部分的 UITableView 中删除行

iphone - 无法将 iOS/iPhone 应用程序构建为 i386 以外的任何应用程序

iPhone - 获取锁屏背景图片

iphone - 手动同步 iCloud

iphone - UITextView不显示滚动条

iphone - awakeFromNib 不是调用而是显示 xib

iphone - iCloud 会保留属于已删除应用程序的用户数据吗?