iOS.데이터 영구화.PersistenceLayer.객체 아카이빙
13344 단어 ios
#import <Foundation/Foundation.h>
#import "Persistence02Note.h"
#define FILE_NAME @"Persistence02NotesList.archive"
#define ARCHIVE_KEY @"Persistence02NotesList"
@interface Persistence02NoteDAO : NSObject
+ (Persistence02NoteDAO*)sharedManager;
- (NSString *)applicationDocumentsDirectoryFile;
- (void)createEditableCopyOfDatabaseIfNeeded;
// Persistence02Note
-(int) create:(Persistence02Note*)model;
// Persistence02Note
-(int) remove:(Persistence02Note*)model;
// Persistence02Note
-(int) modify:(Persistence02Note*)model;
//
-(NSMutableArray*) findAll;
//
-(Persistence02Note*) findById:(Persistence02Note*)model;
@end
#import "Persistence02NoteDAO.h"
@implementation Persistence02NoteDAO
static Persistence02NoteDAO *sharedManager = nil;
+ (Persistence02NoteDAO*)sharedManager
{
static dispatch_once_t once;
dispatch_once(&once, ^{
sharedManager = [[self alloc] init];
[sharedManager createEditableCopyOfDatabaseIfNeeded];
});
return sharedManager;
}
- (void)createEditableCopyOfDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *writableDBPath = [self applicationDocumentsDirectoryFile];
BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];
if (!dbexits) {
NSString *path = [self applicationDocumentsDirectoryFile];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1 = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
Persistence02Note* Persistence02Note1 = [[Persistence02Note alloc] init];
Persistence02Note1.date = date1;
Persistence02Note1.content = @"Welcome to MyPersistence02Note.";
NSDate *date2 = [dateFormatter dateFromString:@"2011-12-04 16:01:03"];
Persistence02Note* Persistence02Note2 = [[Persistence02Note alloc] init];
Persistence02Note2.date = date2;
Persistence02Note2.content = @" MyPersistence02Note。";
NSMutableArray* array = [[NSMutableArray alloc] init];
[array addObject:Persistence02Note1];
[array addObject:Persistence02Note2];
NSMutableData * theData = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:theData];
[archiver encodeObject:array forKey:ARCHIVE_KEY];
[archiver finishEncoding];
[theData writeToFile:path atomically:YES];
}
}
- (NSString *)applicationDocumentsDirectoryFile {
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentDirectory stringByAppendingPathComponent:FILE_NAME];
return path;
}
// Persistence02Note
-(int) create:(Persistence02Note*)model
{
NSString *path = [self applicationDocumentsDirectoryFile];
NSMutableArray *array = [self findAll];
[array addObject:model];
NSMutableData * theData = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:theData];
[archiver encodeObject:array forKey:ARCHIVE_KEY];
[archiver finishEncoding];
[theData writeToFile:path atomically:YES];
return 0;
}
// Persistence02Note
-(int) remove:(Persistence02Note*)model
{
NSString *path = [self applicationDocumentsDirectoryFile];
NSMutableArray *array = [self findAll];
for (Persistence02Note* Persistence02Note in array) {
//
if ([Persistence02Note.date isEqualToDate:model.date]){
[array removeObject: Persistence02Note];
NSMutableData * theData = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:theData];
[archiver encodeObject:array forKey:ARCHIVE_KEY];
[archiver finishEncoding];
[theData writeToFile:path atomically:YES];
break;
}
}
return 0;
}
// Persistence02Note
-(int) modify:(Persistence02Note*)model
{
NSString *path = [self applicationDocumentsDirectoryFile];
NSMutableArray *array = [self findAll];
for (Persistence02Note* Persistence02Note in array) {
//
if ([Persistence02Note.date isEqualToDate:model.date]){
Persistence02Note.content = model.content;
NSMutableData * theData = [NSMutableData data];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc]
initForWritingWithMutableData:theData];
[archiver encodeObject:array forKey:ARCHIVE_KEY];
[archiver finishEncoding];
[theData writeToFile:path atomically:YES];
break;
}
}
return 0;
}
//
-(NSMutableArray*) findAll
{
NSString *path = [self applicationDocumentsDirectoryFile];
NSMutableArray *listData = [[NSMutableArray alloc] init];
NSData * theData =[NSData dataWithContentsOfFile:path];
if([theData length] > 0) {
NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:theData];
listData = [archiver decodeObjectForKey:ARCHIVE_KEY];
[archiver finishDecoding];
}
return listData;
}
//
-(Persistence02Note*) findById:(Persistence02Note*)model
{
NSString *path = [self applicationDocumentsDirectoryFile];
NSMutableArray *listData = [[NSMutableArray alloc] init];
NSData * theData =[NSData dataWithContentsOfFile:path];
if([theData length] > 0) {
NSKeyedUnarchiver * archiver = [[NSKeyedUnarchiver alloc]
initForReadingWithData:theData];
listData = [archiver decodeObjectForKey:ARCHIVE_KEY];
[archiver finishDecoding];
for (Persistence02Note* Persistence02Note in listData) {
//
if ([Persistence02Note.date isEqualToDate:model.date]){
return Persistence02Note;
}
}
}
return nil;
}
@end
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.