【问题标题】:want to fetch data from array of dictionary from plist in iPhone? [duplicate]想要从 iPhone 的 plist 中的字典数组中获取数据? [复制]
【发布时间】:2014-02-21 09:39:01
【问题描述】:

我是 iPhone 新手,根据要求,我必须从 tableview 的问题列表中选择一个问题,并希望在下一个新视图中显示该问题。所以我将我的行值发送到新的 Xib 文件创造的时间。

我将问题答案存储在 plist 中作为字典格式的数组。我可以从 Plist 中获取数组,但无法获取字典数据。我创建了自定义类来存储问题答案。但是在新的 get只有存储在 plist 中的最后一个问题。 请帮助我。谢谢你..

在这里我附上我的代码tableView.m,newView.mcustomClass.m..

tableView.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

            NSLog(@" index row value:%d",indexPath.row);
           NSInteger row=indexPath.row;
            questionViewController *viewController1 =
            [[questionViewController alloc] initWithNibName:@"questionViewController" bundle:[NSBundle mainBundle]];
            viewController1.rowValue=row;
                self.viewController = viewController1;
            [self.viewController.navigationItem setTitle:@"Ques Details"];


        [self.navigationController pushViewController:self.viewController animated:YES];
    }

在新的 Xib 文件中,在 ViewDidLoadFunction 中

newView.m

    - (void)viewDidLoad
    {
      //rowValue tells me which row is selected
        [self firstView:rowValue];

    }
    -(void)firstView: (NSInteger)indexNumber{
        NSString *thePath = [[NSBundle mainBundle]  pathForResource:@"Qus_ans" ofType:@"plist"];
        NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
        NSDictionary *eachElement;
     //overlap the data here   
     for (eachElement in rawElementsArray)
        {
           element = [[Question alloc] initWithDictionary:eachElement];}
   //get only last question in the plist
   questionLabel.text =[NSString stringWithFormat:@"%@",element.ques];

     }
    element is my Custom class object.This is my custom class:
    Question.m

    - (id)initWithDictionary:(NSDictionary *)aDictionary {

        self = [[Question alloc] init];
        if (self) {

            self.ques = [aDictionary valueForKey:@"question"];
            self.ans = [aDictionary valueForKey:@"answer"];

            [self data];
        }
        return self;
    }

    Here element.ques is overlapped with the last value.
    How I get proper question without using the forLoop...

这是我的 plist 格式

<array>
    <dict>
        <key>question</key>
        <string>Approximately how much does the human brain weigh?</string>
        <key>answer</key>
        <string>Three pounds</string>
    </dict>
    <dict>
        <key>question</key>
        <string>The brain has this number of The brain has this number of nerve cells?</string>
        <key>answer</key>
        <string>1 to 100 billion</string>
    </dict>

【问题讨论】:

  • 请格式化您的问题。很难阅读。
  • 这就是为什么我之前提到我是 iPhone 新手,但我想我清楚地提到了每个功能的用途。
  • 此链接帮助。在发布搜索 stackoverflow 天气之前,这个问题是否可用
  • 谢谢先生。但我想要类似的东西 - 我正在从 didSelectRowAtIndexPath 中选择特定数据,并希望从 plist.through 的字典数组中获取特定行号字典的键值循环我可以获取 plist 的全部数据,但是我如何从字典中存储特定的行键。

标签: ios iphone plist


【解决方案1】:

好吧,我不完全确定我理解你的问题,但如果你想摆脱你的 for 循环(我猜是这种情况),你可以简单地写

NSArray *rawElementsArray = [[NSArray alloc] initWithContentsOfFile:thePath];
NSDictionary * dic = rawElementsArray objectAtIndex:rowNumber];

【讨论】:

  • 谢谢先生。它正在工作。我在我的代码中犯了一个错误。我写了 NSString *versionString = [rawElementsArray objectAtIndex:indexNumber];我的代码中的这一行。每次都会出错。
  • 非常感谢 EsbenB。
猜你喜欢
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
相关资源
最近更新 更多