Lecture des données d'un fichier

npanti

Membre enregistré
17 Mai 2009
1
0
Salut à tous,

Je suis occupé à lire "Cocoa Programming for Mac OS X" et j'essaye de faire le challenge 18.
Je dois créer un programme (document-based) qui permet de dessiner une forme ovale avec la souris dans une Custon View et je dois aussi pouvoir enregistrer le document.

Le problème c'est que lorsque j'ouvre un fichier rien ne s'affiche les ovales enregistrés n'apparaissent pas.

J'ai vérifié, mon fichier enregistré comprend bien le tout les objets contenu dans bezierPaths

Merci d'avance pour votre aide

Nicolas

Voici le code:
mydocument.h
Bloc de code:
#import <Cocoa/Cocoa.h>
@class StretchView;

@interface MyDocument : NSDocument
{
	IBOutlet StretchView *stretchView;
}

@end

mydocument.m
Bloc de code:
#import "MyDocument.h"
#import "StretchView.h"

@implementation MyDocument

- (id)init
{
    self = [super init];
    if (self) {
    
        // Add your subclass-specific initialization here.
        // If an error occurs here, send a [self release] message and return nil.
    
    }
    return self;
}

#pragma mark window

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
    [[stretchView window] endEditingFor:nil];
	
	return [NSKeyedArchiver archivedDataWithRootObject:[stretchView bezierPaths]];
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
	NSMutableArray *newBezierPaths = nil;
	
	@try {
		newBezierPaths = [NSKeyedUnarchiver unarchiveObjectWithData:data];
	}
    @catch (NSException *e) {
		if (outError) {
			NSDictionary *d = [NSDictionary dictionaryWithObject:@"The data is corrupted." 
														  forKey:NSLocalizedFailureReasonErrorKey];
			*outError = [NSError errorWithDomain:NSOSStatusErrorDomain 
											code:unimpErr 
										userInfo:d];
		}
	}
	
	[stretchView setBezierPaths:newBezierPaths];
    return YES;
}

@end

stretchview.h
Bloc de code:
#import <Cocoa/Cocoa.h>


@interface StretchView : NSView {
	
	NSMutableArray *bezierPaths;
	NSPoint firstPoint;
	NSPoint currentPoint;
}
- (NSRect)convertToRect;
- (void)drawOval:(NSRect)ovalRect;

@property (readwrite, retain) NSMutableArray *bezierPaths;
@end

stretchview.m
Bloc de code:
#import "StretchView.h"


@implementation StretchView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		if ([bezierPaths count]) {
			for (NSBezierPath *sPath in bezierPaths)
			{
				[sPath fill];
			}
		}
		else {
			bezierPaths = [[NSMutableArray alloc] init];
		}
		
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
	NSRect bounds = [self bounds];
	[[NSColor greenColor] set];
	[NSBezierPath fillRect:bounds];
	
	[[NSColor blackColor] set];
	[self drawOval:[self convertToRect]];
	
	for (NSBezierPath *sPath in bezierPaths)
	{
		[sPath fill];
	}
	
}

- (void)drawOval:(NSRect)ovalRect
{
	NSBezierPath *thePath = [NSBezierPath bezierPathWithOvalInRect:ovalRect];
	[bezierPaths addObject:thePath];
}

- (NSRect)convertToRect
{
	float minX = MIN(firstPoint.x, currentPoint.x);
	float minY = MIN(firstPoint.y, currentPoint.y);
	float maxX = MAX(firstPoint.x, currentPoint.x);
	float maxY = MAX(firstPoint.y, currentPoint.y);
	
	return NSMakeRect(minX, minY, maxX - minX, maxY - minY); 
}

#pragma mark mouseGesture

- (void)mouseDown:(NSEvent *)event
{
	NSPoint p = [event locationInWindow];
	firstPoint = [self convertPoint:p fromView:nil];
	currentPoint = firstPoint;
}
					
- (void)mouseDragged:(NSEvent *)event
{
	NSPoint p = [event locationInWindow];
	currentPoint = [self convertPoint:p fromView:nil];
}
					
- (void)mouseUp:(NSEvent *)event
{
	NSPoint p = [event locationInWindow];
	currentPoint = [self convertPoint:p fromView:nil];
	[self setNeedsDisplay:YES];
	
}

@synthesize bezierPaths;

@end