2010-07-02 4 views
0

다음 코드를 사용하여 현재 로컬 알림 목록을 유지하려고합니다. NSArray는 명시 적으로 작업 할 객체의 종류를 나열합니다. 이는 UILocalNotification 객체로 구성된 배열로 사용할 수 없다는 것을 의미합니다. 그러나 UILocalNotifications는 NSCoding을 구현하므로이 객체 목록을 serialize/deserialize 할 수있는 쉬운 방법이 있어야합니다. 인코딩과 파일 지속성을 직접 수행해야합니까? 또한 쓰기가 실패한 이유에 대한 자세한 정보를 얻을 수있는 방법이 있습니까? 슬래시 (/)를 보장 할 파일 이름 앞에 필요한 경우, 포함되어디스크에 UILocalNotifications의 배열을 쓸 수 있습니까?

- (NSString*)getSavedNotifsPath { 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    return [documentsDirectory stringByAppendingString:@"saved_notifs.plist"]; 
} 

- (void)prepareToHide { 
UIApplication* app = [UIApplication sharedApplication]; 
NSArray *existingNotifications = [app scheduledLocalNotifications]; 
if (! [existingNotifications writeToFile:[self getSavedNotifsPath] atomically:NO]) { 
    // alert 
    [self showSomething:@"write failed"]; 
} 
} 

답변

2

첫째,

return [documentsDirectory stringByAppendingPathComponent:@"saved_notifs.plist"]; 

stringByAppendingPathComponent에 코드

return [documentsDirectory stringByAppendingString:@"saved_notifs.plist"]; 

을 변경합니다.

NSArray는 UILocalNotification이 아닌 속성 목록 개체 만 저장할 수 있습니다. 대신 NSKeyedArchiver를 사용해보십시오. 예 :

- (void)prepareToHide { 
    UIApplication* app = [UIApplication sharedApplication]; 
    NSArray *existingNotifications = [app scheduledLocalNotifications]; 
    NSString *path = [self getSavedNotifsPath]; 
    BOOL success = [NSKeyedArchiver archiveRootObject:existingNotifications toFile:path]; 
    if (! success) { 
     // alert 
     [self showSomething:@"write failed"]; 
    } 
} 

NSKeyedUnarchiver를 사용하여 저장된 파일에서 배열을 검색합니다.

참고 : 실제로 테스트를 해본 결과 100 % 확신 할 수는 없습니다. 그러나 시도하고 어떤 일이 일어나는 지보십시오.

+0

쿨, 고마워 커비! 나는 NSKeyedArchiver가 존재하는지 몰랐다. NSKeyedUnarchiver와 쌍을 이루면 효과가 있습니다. 감사! –

관련 문제