从plist创建NSArray

时间:2011-04-24 15:00:07

标签: iphone objective-c ios

我试图从plist创建一个NSArray但由于某种原因,数组永远不会被初始化。

继承代码 -

NSString * path = [[NSBundle mainBundle] pathForResource:@"defaultWorkouts2" ofType:@"plist"];

    NSLog(@" ------ %@", path);

    NSArray * defaultWorkouts = [[NSArray alloc] initWithContentsOfFile:path];

    NSLog(@" ------ %@", defaultWorkouts);

继承控制台输出 -

2011-04-24 16:17:26.732 IntervalTimer[9778:207] ------ file://localhost/Users/darrenfindlay/Library/Application%20Support/iPhone%20Simulator/4.3/Applications/8FAC4993-D1BA-460F-9A56-8A6984CB8AB6/IntervalTimer.app/defaultWorkouts.plist 2011-04-24 16:17:26.733 IntervalTimer[9778:207] ------ (null))

这是plist文件 -

<?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>Boxing</key>
    <array>
        <integer>8</integer>
        <string>Sparring</string>
        <array>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
        </array>
        <array>
            <string>Round 1</string>
            <string>Rest, Round 2 next</string>
            <string>Round 2</string>
            <string>Rest, Round 3 next</string>
            <string>Round 3</string>
            <string>Rest, Round 4 next</string>
            <string>Round 4</string>
            <string>Rest, Round 5 next</string>
            <string>Round 5</string>
            <string>Rest, Round 6 next</string>
            <string>Round 6</string>
            <string>Rest, Round 7 next</string>
            <string>Round 7</string>
            <string>Rest, Round 8 next</string>
            <string>Round 8</string>
        </array>
    </array>
    <key>Boxing - 2</key>
    <array>
        <integer>8</integer>
        <string>Sparring</string>
        <array>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
        </array>
        <array>
            <string>Round 1</string>
            <string>Rest, Round 2 next</string>
            <string>Round 2</string>
            <string>Rest, Round 3 next</string>
            <string>Round 3</string>
            <string>Rest, Round 4 next</string>
            <string>Round 4</string>
            <string>Rest, Round 5 next</string>
            <string>Round 5</string>
            <string>Rest, Round 6 next</string>
            <string>Round 6</string>
            <string>Rest, Round 7 next</string>
            <string>Round 7</string>
            <string>Rest, Round 8 next</string>
            <string>Round 8</string>
        </array>
    </array>
    <key>Boxing - 3</key>
    <array>
        <integer>8</integer>
        <string>Sparring</string>
        <array>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
            <integer>60</integer>
            <integer>150</integer>
        </array>
        <array>
            <string>Round 1</string>
            <string>Rest, Round 2 next</string>
            <string>Round 2</string>
            <string>Rest, Round 3 next</string>
            <string>Round 3</string>
            <string>Rest, Round 4 next</string>
            <string>Round 4</string>
            <string>Rest, Round 5 next</string>
            <string>Round 5</string>
            <string>Rest, Round 6 next</string>
            <string>Round 6</string>
            <string>Rest, Round 7 next</string>
            <string>Round 7</string>
            <string>Rest, Round 8 next</string>
            <string>Round 8</string>
        </array>
    </array>
</dict>
</plist>

提前感谢您的帮助,这让我发疯了。

1 个答案:

答案 0 :(得分:3)

如果您查看plist文件,则会显示NSDictionary而不是NSArray。您应该将实施更改为:

NSDictionary * defaultWorkouts = [[NSDictionary alloc] initWithContentsOfFile:path];