如何获取此NSDictionary的值

时间:2015-07-17 16:56:41

标签: ios objective-c nsarray nsdictionary

我有一个传入的字典,如下所示: (这是更新的)

{
"StationFavorites.plist" =     {
    "Addison Road" =         {
        Code = G03;
        Lat = "38.8867478168";
        Line = BL;
        Lon = "-76.89410791";
        Name = "Addison Road";
        Type = 1;
    };
    Anacostia =         {
        Code = F06;
        Lat = "38.8629631168";
        Line = GR;
        Lon = "-76.9953707387";
        Name = Anacostia;
        Type = 1;
    };
    Archives =         {
        Code = F02;
        Lat = "38.8936652235";
        Line = GR;
        Lon = "-77.0219143879";
        Name = Archives;
        Type = 1;
    };
};

}

我试图将其转换为数组:

NSArray *array = [dictionary allValues];

但我最终得到阵列中的一个项目,我想要三个。我该怎么做?

更新:根据要求,这是我当前的代码片段。

- (void)awakeWithContext:(id)context {

    [super awakeWithContext:context];
    if ([WCSession isSupported]) {
        NSLog(@"session isSupported...");
        self.session = [WCSession defaultSession];
        self.session.delegate = self;
        [self.session activateSession];

        NSError *error = nil;
        NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documents = [directories firstObject];

        pathString = [documents stringByAppendingPathComponent:@"Favorites.plist"];
        NSLog(@"pathString > %@", pathString);

        NSFileManager *fileManager = [NSFileManager defaultManager];
        if (![fileManager fileExistsAtPath: pathString]) //4
        {
            NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Favorites" ofType:@"plist"];

            [fileManager copyItemAtPath:bundle toPath: pathString error:&error];
        }

        if ([[NSFileManager defaultManager] fileExistsAtPath:pathString]) {
            [self readFromFile:pathString];
            NSMutableDictionary  *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:pathString];
            //NSLog(@" count: %lu", (unsigned long)dictionary.count);
            NSLog(@" dictionary: %@", dictionary);

            NSArray *tempArray = [dictionary allValues];
            self.workingArray = [tempArray mutableCopy];

            [self configureTable];
        }

    }
}

2 个答案:

答案 0 :(得分:4)

这三个值位于名为“StationFavourites.plist”的键下

所以尝试类似:

[(NSDictionary *)dictionary[@"StationFavourites.plist"] allValues];

答案 1 :(得分:2)

 for (NSString *key in [dictionary allKeys]) {
        NSDictionary *currDict = dictionary[key];
       // you can create here from the valueDict a new Object and store it for example in an Array
 }