我是Objective-C的新手
我想读一个文件。
/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"]];
完整代码:
#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
你会得到容器中Documents文件夹的URL
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