0

こんにちは、plist から値を読み込もうとしています。これは私の plist が atm のように見えるものです。

<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Mickey Mouse</string>
        <key>Image</key>
        <string>mickeymouse.png</string>
        <key>Hint1</key>
        <string>Hint 1</string>
        <key>Hint2</key>
        <string>Hint 2</string>
        <key>Hint3</key>
        <string>Hint 3</string>
    </dict>
    <dict>
        <key>Title</key>
        <string>Peter Pan</string>
        <key>Image</key>
        <string>peterpan.png</string>
        <key>Hint1</key>
        <string>Hint 1</string>
        <key>Hint2</key>
        <string>Hint 2</string>
        <key>Hint3</key>
        <string>Hint 3</string>
    </dict>
</array>
</plist>

これは私が使用しているコードですが、クラッシュするだけです。

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *questionNumber = [NSMutableString stringWithFormat:@"Item 0"];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Level1" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSDictionary *questionNumberDictionary = [array objectForKey:questionNumber];
NSString *questionTitle = [questionNumberDictionary objectForKey:@"Title"];
NSString *questionImage = [questionNumberDictionary objectForKey:@"Image"];
NSString *questionHint1 = [questionNumberDictionary objectForKey:@"Hint1"];
NSString *questionHint2 = [questionNumberDictionary objectForKey:@"Hint2"];
NSString *questionHint3 = [questionNumberDictionary objectForKey:@"Hint3"];
4

1 に答える 1

7

次の行に問題があります

NSDictionary *questionNumberDictionary = [array objectForKey:questionNumber];

forループを使用して配列を反復処理する必要がある代わりに

 NSString *path = [[NSBundle mainBundle] pathForResource:@"Level1" ofType:@"plist"];
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

    for (NSDictionary *dict in array) {
        NSString *questionTitle = [dict objectForKey:@"Title"];
        NSString *questionImage = [dict objectForKey:@"Image"];
        NSString *questionHint1 = [dict objectForKey:@"Hint1"];
        NSString *questionHint2 = [dict objectForKey:@"Hint2"];
        NSString *questionHint3 = [dict objectForKey:@"Hint3"];

    }

これで問題は解決します。

于 2013-01-08T15:42:18.680 に答える