我将字典的 plist 数组复制到设备上的文档目录中,如下所示:
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self performSelector:@selector(copyPlist)];
return YES;
}
- (void)copyPlist {
NSError *error;
NSFileManager *fileManager=[NSFileManager defaultManager];
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"Wine.plist"];
if ([fileManager fileExistsAtPath:destinationPath]){
NSLog(@"database localtion %@",destinationPath);
return;
}
NSString *sourcePath= [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
NSLog(@"source path %@",sourcePath);
[fileManager copyItemAtPath:sourcePath toPath:destinationPath error:&error];
}
数据库位置的日志:
/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist
然后,我尝试访问 plist 并用字典填充 sortedWines 数组以填充 TableView:
表视图控制器.h
#import <UIKit/UIKit.h>
@interface WinesViewController : UITableViewController <UIActionSheetDelegate> {
NSMutableArray *sortedWines;
}
@end
表视图控制器.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Wine.plist"];
NSLog(@"plist path %@", path);
sortedWines = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"objects %@", sortedWines);
NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Popularity" ascending:YES];
[sortedWines sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
[super viewDidLoad];
}
plist 路径的日志:
/var/mobile/Applications/832E16F4-A204-457E-BFF0-6AEA27915C25/Documents/Wine.plist
和对象的日志:
(无效的)
现在如何用对象填充 sortedWines 数组?