Created
March 16, 2011 19:47
-
-
Save mistersourcerer/873173 to your computer and use it in GitHub Desktop.
NSDictionary / NSMutableDictionary plist file persistence
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
#import <Foundation/Foundation.h> | |
@interface NSDictionary(Persistence) | |
-(void)writeToFileInDocumentsDir:(NSString *)path atomically:(BOOL)atomically; | |
@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
#import "NSDictionary+Persistence.h" | |
@implementation NSDictionary(Persistence) | |
-(void)writeToFileInDocumentsDir:(NSString *)path atomically:(BOOL)atomically { | |
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *finalPath = [docsDir stringByAppendingPathComponent:path]; | |
NSString *dirTree = [finalPath stringByDeletingLastPathComponent]; | |
NSFileManager *fm = [NSFileManager defaultManager]; | |
BOOL isDir = NO; | |
if (![fm fileExistsAtPath:dirTree isDirectory:&isDir]) { | |
[fm createDirectoryAtPath:dirTree withIntermediateDirectories:YES attributes:nil error:nil]; | |
} | |
[self writeToFile:finalPath atomically:YES]; | |
} | |
@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
#import "GTMSenTestCase.h" | |
#import "NSDictionary+Persistence.h" | |
@interface NSDictionary_PersistenceTests : GTMTestCase { | |
NSString *docsDir; | |
NSString *filePath; | |
NSString *fileName; | |
NSDictionary *dic; | |
} | |
@end | |
@implementation NSDictionary_PersistenceTests | |
#pragma mark - | |
-(void)setUp { | |
docsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] retain]; | |
fileName = @"file.plist"; | |
filePath = [[docsDir stringByAppendingPathComponent:fileName] retain]; | |
dic = [[NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", nil] retain]; | |
} | |
-(void)tearDown { | |
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil]; | |
[filePath release]; | |
[fileName release]; | |
[dic release]; | |
[docsDir release]; | |
} | |
// Should | |
#pragma mark - | |
- (void) test_allow_save_dictionary_contents_in_a_plist_on_docs_dir { | |
[dic writeToFileInDocumentsDir:fileName atomically:YES]; | |
BOOL isFileCreated = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; | |
STAssertTrue(isFileCreated, @"The file should being created."); | |
} | |
- (void) test_create_subdirs_when_is_needed { | |
NSString *subdirs = @"Teste"; | |
subdirs = [subdirs stringByAppendingPathComponent:@"ComSubdirs"]; | |
NSString *newPath = [subdirs stringByAppendingPathComponent:fileName]; | |
[dic writeToFileInDocumentsDir:newPath atomically:YES]; | |
NSString *finalPath = [docsDir stringByAppendingPathComponent:newPath]; | |
BOOL isFileCreated = [[NSFileManager defaultManager] fileExistsAtPath:finalPath]; | |
STAssertTrue(isFileCreated, @"The file should being created."); | |
if (isFileCreated) { | |
[[NSFileManager defaultManager] removeItemAtPath:finalPath error:nil]; | |
} | |
} | |
@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
#import <Foundation/Foundation.h> | |
@interface NSMutableDictionary(Persistence) | |
NSString *currentFilePath; | |
+(NSMutableDictionary *)dictionaryWithContentsOfFileInDocumentsDir:(NSString *)path; | |
-(void)setCurrentFilePath:(NSString *)filePath; | |
-(void)write; | |
@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
#import "NSMutableDictionary+Persistence.h" | |
@implementation NSMutableDictionary(Persistence) | |
+(NSMutableDictionary *)dictionaryWithContentsOfFileInDocumentsDir:(NSString *)path { | |
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *filePath = [docsDir stringByAppendingPathComponent:path]; | |
NSFileManager *fm = [NSFileManager defaultManager]; | |
NSMutableDictionary *dic = [NSMutableDictionary dictionary]; | |
[dic setCurrentFilePath:filePath]; | |
BOOL fileAlreadyExists = [fm fileExistsAtPath:filePath]; | |
if (fileAlreadyExists) { | |
dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; | |
} | |
return dic; | |
} | |
-(void)setCurrentFilePath:(NSString *)filePath { | |
if (currentFilePath != filePath) { | |
[currentFilePath release]; | |
currentFilePath = [filePath retain]; | |
} | |
} | |
-(void)write { | |
[self writeToFile:currentFilePath atomically:YES]; | |
[self setCurrentFilePath:nil]; | |
} | |
@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
#import "GTMSenTestCase.h" | |
#import "NSMutableDictionary+Persistence.h" | |
@interface NSMutableDictionary_PersistenceTests : GTMTestCase { | |
NSString *tempDir; | |
NSString *docsDir; | |
} | |
@end | |
@implementation NSMutableDictionary_PersistenceTests | |
- (void) setUp { | |
tempDir = [NSTemporaryDirectory() retain]; | |
docsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] retain]; | |
[tempDir retain]; | |
} | |
- (void) tearDown { | |
[tempDir release]; | |
[docsDir release]; | |
} | |
// Should | |
- (void) test_return_new_mutable_dictionary_if_file_doesnt_exists { | |
NSString *inexistentFile = [tempDir stringByAppendingPathComponent:@"fake.file"]; | |
NSMutableDictionary *session = [NSMutableDictionary dictionaryWithContentsOfFileInDocumentsDir:inexistentFile]; | |
BOOL isNewDictionary = [session isEqualToDictionary:[NSMutableDictionary dictionary]]; | |
STAssertTrue(isNewDictionary, @"Should have created a new NSMutableDictionary"); | |
} | |
- (void) test_return_mutable_dictionary_with_contents_of_existent_file { | |
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: | |
@"um", @"1", | |
@"dois", @"2", | |
@"três", @"3", | |
nil]; | |
NSString *fileName = @"nsmutabledic_test_file.plist"; | |
NSString *dicFile = [docsDir stringByAppendingPathComponent:fileName]; | |
[dic writeToFile:dicFile atomically:YES]; | |
NSMutableDictionary *mdic = [NSMutableDictionary dictionaryWithContentsOfFileInDocumentsDir:fileName]; | |
BOOL isFileDictionary = [mdic isEqualToDictionary:dic]; | |
STAssertTrue(isFileDictionary, @"Should have created a NSMutableDictionary with contents of file"); | |
[[NSFileManager defaultManager] removeItemAtPath:dicFile error:nil]; | |
} | |
- (void) test_write_on_new_file_when_return_new_mutableDictionary_from_inexistent_file { | |
NSString *fileName = @"fake.file"; | |
NSMutableDictionary *newDic = [NSMutableDictionary dictionaryWithContentsOfFileInDocumentsDir:fileName]; | |
[newDic setObject:@"um" forKey:@"1"]; | |
[newDic write]; | |
NSString *newDicFile = [docsDir stringByAppendingPathComponent:fileName]; | |
BOOL isFileSaved = [[NSFileManager defaultManager] fileExistsAtPath:newDicFile]; | |
STAssertTrue(isFileSaved, @"The file was not saved."); | |
NSMutableDictionary *existentDic = [NSMutableDictionary dictionaryWithContentsOfFileInDocumentsDir:fileName]; | |
BOOL haveSavedDictionary = [newDic isEqualToDictionary:existentDic]; | |
STAssertTrue(haveSavedDictionary, @"Need save the dictionary on right file using the #write method"); | |
[[NSFileManager defaultManager] removeItemAtPath:[docsDir stringByAppendingPathComponent:fileName] error:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment