使用:XCode 4
讀入與把不存在的檔案複製過去:
NSString *dataname = @"DataName" ;
NSArray *dirpathary = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dirpath = [dirpathary objectAtIndex:0] ;
NSString *path = [dirpath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",dataname] ];
NSFileManager *fileManager = [NSFileManager defaultManager];
//假如在document內沒有這個存檔紀錄的話,那就複製一份內定(?)的過去
if (![fileManager fileExistsAtPath: path])
{
NSError *error;
NSString *file = [[NSBundle mainBundle] pathForResource:dataname ofType:@"plist"];
[fileManager copyItemAtPath:file toPath:path error:&error];
}
NSArray *data = [[NSArray arrayWithContentsOfFile:path] retain];
儲存檔案:
NSArray *data = [NSArray arrayWithObjects:.....,nil];
NSString *dataname = @"DataName" ;
NSArray *dirpathary = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
dirpath = [dirpathary objectAtIndex:0];
NSString *path = [dirpath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",dataname] ];
[data writeToFile:path atomically:NO];
沒有留言:
張貼留言
歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT