Plist中的NSDictionary和NSArray

时间:2012-12-21 12:51:59

标签: objective-c

我想做以下事情:

带有-z和#的plist文件,用于数字或未知字符..所以27个数组。 我想将这27个数组存储在字典中,这是一个plist文件。

也可能发生,字母数组中没有表示字母......

我是否需要制作27个阵列并检查它们是否存在或是否有更简单的方法?

我想在tableview中显示它们..表视图的“a”部分中的键“a”的所有值等等......

任何想法如何节省时间和代码?

感谢我有可能尝试

1 个答案:

答案 0 :(得分:0)

试用此代码

在pList文件中写入数据

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

    NSMutableArray *arr = [NSMutableArray new];

    for (char value = 'a'; value<='z'; value++) {
        [arr addObject:[NSString stringWithFormat:@"%c",value]];
    }

    NSDictionary *dict = [NSDictionary dictionaryWithObject:arr forKey:@"alphabet"];

    [dict writeToFile:filePath atomically:YES]

设置numberOfSectionsInTableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return [[dict objectForKey:@"alphabet"]count];
}

设置numberOfRowsInSection

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return 1;

}

在cellForRowAtIndexPath中设置单元格文本

  [[cell textLabel] setText:[[dict objectForKey:@"alphabet"]objectAtIndex:indexPath.section]];