iOS - 未填充数组 - NSDictionary / NSArray

时间:2014-05-06 10:36:43

标签: ios objective-c nsdictionary plist

我遇到了XCode的问题并且需要很快的帮助......我有一个plist文件,我需要访问它,它应该填充所有可用车辆的数组......不幸的是它没有这样做。 rootPath有一个值。 path有一个值。 settings NOT 有值,它返回为零。为什么是这样?据我所知,这曾经适用于旧版本的iOS / OS / XCode版本。 任何想法都是完美的!以下是plist文件出错的代码:

    -(NSArray *)AllAvailableVehicles {
    // load in vehicles from plist data

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

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                              NSUserDomainMask, YES) objectAtIndex:0];

    NSString *path = [rootPath stringByAppendingPathComponent:@"vehicles.plist"];

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:path];

    NSArray *array = [settings objectForKey:@"Vehicles"];

    NSMutableArray *vehicleMutArray = [NSMutableArray arrayWithCapacity:20];
    for (NSDictionary *dict in array){
        Vehicle *vehicle = [[Vehicle alloc] init];
        vehicle.displayName = [dict objectForKey:@"Display Name"];
        vehicle.keyName = [dict objectForKey:@"Key Name"];
        vehicle.maxPassengers = [[dict objectForKey:@"Max Passengers"] intValue];
        vehicle.cabCode = [dict objectForKey:@"Cabmaster Code"];
        vehicle.luggageImage = [dict objectForKey:@"Image"];
        [vehicleMutArray addObject:vehicle];
        [vehicle release];
    }
    NSArray *vehiclesArray = [[vehicleMutArray copy] autorelease];
    return vehiclesArray;

}

plist文件:     

<!DOCTYPE plist SYSTEM "http://www.apple.com/DTDs/PropertyList-1.0.dtd" PUBLIC "-//Apple//DTD PLIST 1.0//EN">

-<plist version="1.0">


-<dict>

<key>Vehicles</key>


-<array>


-<dict>

<key>Display Name</key>

<string>Saloon Vehicle - 4 Seats</string>

<key>Key Name</key>

<string>Standard</string>

<key>Max Passengers</key>

<integer>4</integer>

<key>Cabmaster Code</key>

<string> </string>

<key>Image</key>

<string>Standard.png</string>

</dict>


-<dict>

<key>Display Name</key>

<string>Estate Car - 4 Seats</string>

<key>Key Name</key>

<string>Estate</string>

<key>Max Passengers</key>

<integer>4</integer>

<key>Cabmaster Code</key>

<string>E</string>

<key>Image</key>

<string>Estate.png</string>

</dict>


-<dict>

<key>Display Name</key>

<string>VIP - 4 seats</string>

<key>Key Name</key>

<string>VIP</string>

<key>Max Passengers</key>

<integer>4</integer>

<key>Cabmaster Code</key>

<string>V</string>

<key>Image</key>

<string>Exec_Car.png</string>

</dict>


-<dict>

<key>Display Name</key>

<string>MPV - 6 Seater</string>

<key>Key Name</key>

<string>MPV - 6</string>

<key>Max Passengers</key>

<integer>6</integer>

<key>Cabmaster Code</key>

<string>M</string>

<key>Image</key>

<string>6Seater.png</string>

</dict>


-<dict>

<key>Display Name</key>

<string>MPV - 7 Seater</string>

<key>Key Name</key>

<string>MPV - 7</string>

<key>Max Passengers</key>

<integer>7</integer>

<key>Cabmaster Code</key>

<string>N</string>

<key>Image</key>

<string>7Seater.png</string>

</dict>


-<dict>

<key>Display Name</key>

<string>Minibus - 8 seats</string>

<key>Key Name</key>

<string>Minibus</string>

<key>Max Passengers</key>

<integer>8</integer>

<key>Cabmaster Code</key>

<string>O</string>

<key>Image</key>

<string>Mini.png</string>

</dict>

</array>

</dict>

</plist>

2 个答案:

答案 0 :(得分:0)

替换

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                          NSUserDomainMask, YES) objectAtIndex:0];

NSString *path = [rootPath stringByAppendingPathComponent:@"vehicles.plist"];

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

答案 1 :(得分:0)

我已经解决了这个问题!事实证明我有一些构建规则,这意味着Plist文件不会复制,我删除了所有的构建规则,它的工作原理!