Skip to content

Instantly share code, notes, and snippets.

@bob-codingdutchmen
Created October 7, 2013 15:31
Show Gist options
  • Save bob-codingdutchmen/6869871 to your computer and use it in GitHub Desktop.
Save bob-codingdutchmen/6869871 to your computer and use it in GitHub Desktop.
Basic setup for a test project for debugging lazy loading / saving with NSFileWrappers in a Document based application
//
// Document.m
// Wrappers
//
#import "Document.h"
#define FileName01 @"testfile1.txt"
#define FileName02 @"testfile2.txt"
@interface Document () {
}
@property (nonatomic, retain) NSFileWrapper *myWrapper;
//@property (nonatomic, retain) NSFileWrapper *subDir;
@end
@implementation Document
- (id)init {
self = [super init];
if (self) {
}
return self;
}
- (NSString *)windowNibName {
return @"Document";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
[super windowControllerDidLoadNib:aController];
}
+ (BOOL)autosavesInPlace {
return YES;
}
/**
* Only called when initializing a NEW document
*/
-(id)initWithType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
self = [self init];
if (self) {
self.myWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
NSLog(@"Initializing new document...");
NSString *testString1 = @"Lorem ipsum first sub file";
NSString *testString2 = @"This is the second sub file with completely unrelated contents";
NSFileWrapper *w1 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString1 dataUsingEncoding:NSUTF8StringEncoding]];
NSFileWrapper *w2 = [[NSFileWrapper alloc] initRegularFileWithContents:[testString2 dataUsingEncoding:NSUTF8StringEncoding]];
w1.preferredFilename = FileName01;
w2.preferredFilename = FileName02;
[self.myWrapper addFileWrapper:w1];
[self.myWrapper addFileWrapper:w2];
}
return self;
}
-(NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
// This obviously wouldn't happen here normally, but it illustrates
// how the contents of the first file would be replaced
NSFileWrapper *w1 = [self.myWrapper.fileWrappers objectForKey:FileName01];
[self.myWrapper removeFileWrapper:w1];
NSFileWrapper *new1 = [[NSFileWrapper alloc] initRegularFileWithContents:[@"New file contents" dataUsingEncoding:NSUTF8StringEncoding]];
new1.preferredFilename = FileName01;
[self.myWrapper addFileWrapper:new1];
return self.myWrapper;
}
-(BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)typeName error:(NSError *__autoreleasing *)outError {
self.myWrapper = fileWrapper;
return YES;
}
- (IBAction)button1Pressed:(id)sender {
// Read from file1 and show result in field1
NSFileWrapper *w1 = [[self.myWrapper fileWrappers] objectForKey:FileName01];
NSString *string1 = [[NSString alloc] initWithData:w1.regularFileContents encoding:NSUTF8StringEncoding];
[self.field1 setStringValue:string1];
}
- (IBAction)button2Pressed:(id)sender {
// Read from file2 and show result in field2
NSFileWrapper *w2 = [[self.myWrapper fileWrappers] objectForKey:FileName02];
NSString *string2 = [[NSString alloc] initWithData:w2.regularFileContents encoding:NSUTF8StringEncoding];
[self.field2 setStringValue:string2];
}
-(BOOL)isEntireFileLoaded {
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment