저는 앱을 종료 할 때 앱의 여러보기에서 텍스트를 저장해야하는 앱을 만들고 있습니다. 또한 해당 뷰 중 하나에서 모든 데이터를 제거 할 수 있어야하고 앱이 종료되면 모든 뷰가 아직 생성되지 않았을 수 있습니다.iPhone 앱의 여러보기에서 데이터를 저장하는 방법은 무엇입니까?
this post을 읽은 후 처음에는 데이터가로드 될 때 데이터에로드되는 앱 데이터를 관리하는 싱글 톤을 사용하는 것이 좋을 것이라고 생각했습니다. 앱을 종료 할 때 저장했습니다. 그런 다음 각 뷰에서 데이터를 저장해야하는데 싱글 톤으로 설정할 수 있습니다.
나는 그것을 풀어 줬지만 몇 가지 문제가 있었다. 처음에는 속성을 합성하지 않았지만 (컴파일러는 getters와 setter를 만들어야한다고 말했기 때문에 그렇게했습니다.) 이제 내 applicationWIllTerminate : 응용 프로그램이 충돌하고 콘솔에 "프로그램 수신 신호 :"EXC_BAD_ACCESS "kill quit"이라고 표시됩니다.
누구나 내가 잘못하고있는 것을 말해 줄 수 있습니까? 아니면 데이터를 저장하는 더 나은 방법을 제안 할 수 있습니까?
//SavedData.h
#import <Foundation/Foundation.h>
#define kFileName @"appData.plist"
@interface SavedData : NSObject {
NSString *information;
NSString *name;
NSString *email;
NSString *phone;
NSString *mobile;
}
@property(assign) NSString *information;
@property(assign) NSString *name;
@property(assign) NSString *email;
@property(assign) NSString *phone;
@property(assign) NSString *mobile;
+ (SavedData *)singleton;
+ (NSString *)dataFilePath;
+ (void)applicationWillTerminate:(NSNotification *)notification;
@end
//SavedData.m
#import "SavedData.h"
@implementation SavedData
@synthesize information;
@synthesize name;
@synthesize email;
@synthesize phone;
@synthesize mobile;
static SavedData * SavedData_Singleton = nil;
+ (SavedData *)singleton{
if (nil == SavedData_Singleton){
SavedData_Singleton = [[SavedData_Singleton alloc] init];
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSMutableArray * array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
information = [array objectAtIndex:0];
name = [array objectAtIndex:1];
email = [array objectAtIndex:2];
phone = [array objectAtIndex:3];
mobile = [array objectAtIndex:4];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
}
return SavedData_Singleton;
}
+ (NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DocumentsDirectory = [paths objectAtIndex:0];
return [DocumentsDirectory stringByAppendingPathComponent:kFileName];
}
+ (void)applicationWillTerminate:(NSNotification *)notification{
NSLog(@"Application will terminate received");
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:information];
[array addObject:name];
[array addObject:email];
[array addObject:phone];
[array addObject:mobile];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
}
@end
내가 그것을 사용하고자 할 때 그 때 나는
myLabel.text = [SavedData singleton].information;
을 그리고 나는 필드를 변경하는 경우
[SavedData singleton].information = @"my string";
도움이 될 것입니다.
+1 예, 보존 대신 할당이 거의 확실합니다. 할당 된 객체가 응용 프로그램의 전체 수명 동안 중단되는 것은 거의 없습니다. – TechZen
그 링크를 가져 주셔서 감사합니다. 그게 좋겠지 만, 싱글 톤이 처음 생성 될 때 실행하고 싶은 코드를 어디에 넣었는지 여전히 확실하지 않으므로 사용중인 샘플 코드를 보여줄 수 있다면 정말 좋을 것입니다. + (MyClassName *) sharedMyClassName의 구현을해야합니까? 또는 매크로가하는 일이 무엇입니까? – DownUnder