Last active
May 26, 2018 22:17
-
-
Save alexpaul/114524e0487771d66ffcd73c2d1283f2 to your computer and use it in GitHub Desktop.
Uses NSKeyedArchiver and NSKeyedUnarchiver to save and load custom objects (Event) to and from the documents directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PersistenceDataManager.h | |
// Events | |
// | |
// Created by Alex Paul on 5/25/18. | |
// Copyright © 2018 Alex Paul. All rights reserved. | |
// | |
#import "Event.h" | |
@interface PersistenceDataManager: NSObject | |
+ (NSArray *)loadEvents; | |
+ (BOOL)saveEvents:(NSArray *)events; | |
+ (BOOL)saveImageData:(NSData *)data withFilename:(NSString *)filename; | |
+ (UIImage *)imageWithFilename:(NSString *)filename; | |
+ (BOOL)deleteImageWithFilename:(NSString *)filename; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PersistenceDataManager.m | |
// Events | |
// | |
// Created by Alex Paul on 5/25/18. | |
// Copyright © 2018 Alex Paul. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "PersistenceDataManager.h" | |
#import "Event.h" | |
#define FILENAME @"favorites.plist" | |
@interface PersistenceDataManager () | |
@end | |
@implementation PersistenceDataManager | |
#pragma mark Documents Directory | |
+ (NSString *)filepathWithName:(NSString *)filename { | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = paths.firstObject; | |
NSString *path = [documentsDirectory stringByAppendingPathComponent:filename]; | |
return path; | |
} | |
#pragma mark NSKeyedArchiver and NSKeyedUnarchiver | |
+ (BOOL)saveEvents:(NSArray *)events { | |
NSString *path = [self filepathWithName:FILENAME]; | |
BOOL saved = [NSKeyedArchiver archiveRootObject:events toFile:path]; | |
return saved; | |
} | |
+ (NSArray *)loadEvents { | |
NSString *path = [self filepathWithName:FILENAME]; | |
NSArray <Event *> *events = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; | |
return events; | |
} | |
#pragma mark Image Persistence | |
+ (BOOL)saveImageData:(NSData *)data withFilename:(NSString *)filename { | |
NSString *path = [self filepathWithName:filename]; | |
NSError *error; | |
BOOL saved = [data writeToFile:path options:NSDataWritingAtomic error:&error]; | |
if (error) | |
saved = FALSE; | |
return saved; | |
} | |
+ (BOOL)deleteImageWithFilename:(NSString *)filename { | |
NSString *path = [self filepathWithName:filename]; | |
BOOL deleted = FALSE; | |
if (path) { | |
NSError *error; | |
deleted = [[NSFileManager defaultManager] removeItemAtPath:path error:&error]; | |
if (error) | |
deleted = FALSE; | |
} | |
return deleted; | |
} | |
+ (UIImage *)imageWithFilename:(NSString *)filename { | |
NSString *path = [self filepathWithName:filename]; | |
UIImage *image; | |
if (path) { | |
image = [UIImage imageWithContentsOfFile:path]; | |
} | |
return image; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment