I'm new to the Objective-C
I want to read a file.plist from path Documents:
/4BC1E3AF-15AD-4F33-A55D-E52C60F13DE8/data/Containers/Data/Application/95BD9BE3-5A5A-4EAC-A9FE-133EF64D1D1A/Documents"
Can you help me replace this line of code?:
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"TestPlist" ofType:@"plist"]];
Full code: 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
CodePudding user response:
You get the URL of the Documents folder in the container with
NSURL *documentsFolderURL = [[NSFileManager defaultManager]
URLForDirectory: NSDocumentDirectory
inDomain: NSUserDomainMask
appropriateForURL: nil
create: false
error: nil];
NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfURL:[documentsFolderURL URLByAppendingPathComponent:@"TestPlist.plist"] error: nil];
There is also dictionaryWithContentsOfURL
although I recommend to prefer NSPropertyListSerializtion