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

发布于 2025-02-04 06:43:16 字数 2190 浏览 1 评论 0原文

这是一个 tutorial 如何从

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

文档中读取文件?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

完整代码:

#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

我该怎么做?预先感谢:(

This is a tutorial of how to read a file from :

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

I want to read the file from documents?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

full code:

#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

How can I do that ? and thanks in advance :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2025-02-11 06:43:16

已解决

解决方案后的完整代码:

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    subjectList = [[NSMutableArray alloc]init];
    contentList = [[NSMutableArray alloc]init];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths firstObject];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TestPlist.plist"];
    
    NSURL *aUrl=[NSURL fileURLWithPath:path];
    NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:aUrl];
    
    NSLog(@"%@", path);
    
        NSArray *arrayList = [NSArray arrayWithArray:[aDict 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

Solved

full code after solution :

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    subjectList = [[NSMutableArray alloc]init];
    contentList = [[NSMutableArray alloc]init];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths firstObject];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"TestPlist.plist"];
    
    NSURL *aUrl=[NSURL fileURLWithPath:path];
    NSDictionary *aDict=[[NSDictionary alloc] initWithContentsOfURL:aUrl];
    
    NSLog(@"%@", path);
    
        NSArray *arrayList = [NSArray arrayWithArray:[aDict 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文