如何从 Documents 的 属性 列表中获取数据?

How to get data from property list from Documents?

我是 Objective-C

的新手

我想从路径文档中读取 file.plist:

/4BC1E3AF-15AD-4F33-A55D-E52C60F13DE8/data/Containers/Data/Application/95BD9BE3-5A5A-4EAC-A9FE-133EF64D1D1A/Documents"

你能帮我替换这行代码吗?:

NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"TestPlist" ofType:@"plist"]];

完整代码: here

#import "ViewController.h"

@interface ViewController () {
    NSMutableArray *subjectList;
    NSMutableArray *contentList;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    subjectList = [[NSMutableArray alloc]init];
    contentList = [[NSMutableArray alloc]init];
    
    NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"TestPlist" ofType:@"plist"]];
    NSArray *arrayList = [NSArray arrayWithArray:[dictRoot objectForKey:@"Data"]];
    [arrayList enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
             [subjectList addObject:[obj valueForKey:@"Title"]];
             [contentList addObject:[obj valueForKey:@"Content"]];
        }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [subjectList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if(cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:simpleTableIdentifier];
    }
    
    cell.textLabel.text = [subjectList objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [contentList objectAtIndex:indexPath.row];
    
    return cell;
    
}
@end

你得到URL容器中的Documents文件夹

NSURL *documentsFolderURL = [[NSFileManager defaultManager]
                             URLForDirectory: NSDocumentDirectory
                             inDomain: NSUserDomainMask
                             appropriateForURL: nil
                             create: false
                             error: nil];

NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfURL:[documentsFolderURL URLByAppendingPathComponent:@"TestPlist.plist"] error: nil];

还有dictionaryWithContentsOfURL虽然我建议更喜欢NSPropertyListSerializtion