次のように、辞書の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にデータを入力しようとしています。
TableViewController.h
#import <UIKit/UIKit.h>
@interface WinesViewController : UITableViewController <UIActionSheetDelegate> {
NSMutableArray *sortedWines;
}
@end
TableViewController.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配列をオブジェクトで埋める方法は?