1

質問と回答データを保存するために使用しているプロパティ リスト ファイルがあります。このファイル内には配列のコレクションがあり、これらの中にさまざまなカテゴリと質問を保存しています。しかし、私はこのデータにアクセスできないようですか?

プロパティ ファイル。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Category1</key>
<array>
    <array>
        <string>Question1</string>
        <string>Answer1</string>
    </array>
    <array>
        <string>Question2</string>
        <string>Answer2</string>
    </array>
    <array>
        <string>Question3</string>
        <string>Answer3</string>
    </array>
    <array>
        <string>Question4</string>
        <string>Answer4</string>
    </array>
</array>
</dict>
</plist>

以下では、2 つの単純なボタンを使用してステップスルーすることで、プロパティ リスト ファイルにアクセスするために使用しています。

-(IBAction)nextleft {
    [self bingsound];
    if (questionCounter > 1) {
        questionCounter -= 1;
    }
    [self nextQuestion];
}

-(IBAction)nextright {
    [self bingsound];
     if (questionCounter < 20) {
         questionCounter += 1;
     }
     [self nextQuestion];
}

-(void)nextQuestion {
    NSArray *pair;
    pair = [categories objectAtIndex:questionCounter];
    plistQuestion = [pair objectAtIndex:0];
    plistAnswer = [pair objectAtIndex:1];
    abbreviation.text = plistQuestion;
}

次のようにカテゴリ配列を設定しています。

categories = [NSArray arrayWithContentsOfFile:@"questions.plist"];
4

2 に答える 2

1

arrayWithContentsOfFile:plistファイルへのフルパスを指定する必要があります。以下が機能するはずです。

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"];
categories = [NSArray arrayWithContentsOfFile:filePath];
于 2012-08-23T23:13:26.660 に答える
1

Your property list has a dictionary as the root object, not an array.

Remove these lines from the top:

<dict>
<key>Category1</key>

And this line from the end:

</dict>

また、@murat のアドバイスに従って、プレーンな文字列ではなく、ファイルへの正しいパスを導き出してください。

一般に、このような問題では、デバッガーがあなたの味方です。メソッドにブレークポイントを配置し、各段階 (パスの導出、配列の抽出) で取得する値を確認します。

于 2012-08-24T05:48:09.573 に答える