Aide NSDocument petit logiciel test
Bonjour, je développe un peu en ce moment, et j'ai quelques problèmes de débutant! J'ai joué un peu avec le code de quelques appli open source et j'ai créé un petit TextEdit avec Statistiques du nombre de caractères et du nombre de mots.
Je suis bloqué vu qu'une des lignes de code m'empêche de charger un fichier mais si je l'enlève, les statistiques restent a 0...
Donc voilà , ci quelqu'un a un ptit coup de main a me filer, je lui serai redevable, merci.
#import <Cocoa/Cocoa.h>
#import "KBWordCountingTextStorage.h"
@interface MyDocument : NSDocument
{
IBOutlet NSPanel *statsPanel;
id IBOutlet textView;
IBOutlet NSTextField *wordField;
IBOutlet NSTextField *charField;
NSData *fileData;
KBWordCountingTextStorage *textStorage;
NSAttributedString *loadedText;
}
- (IBAction)orderFrontStatisticsPanel:(id)sender;
@end
_____________________________________________________________
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
if (self = [super init])
{
textStorage = [[KBWordCountingTextStorage alloc] init];
loadedText = nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statisticsDidChange
name:KBTextStorageStatisticsDidChangeNotification
object:textStorage];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[textStorage release];
[loadedText release]; // Just in case
[super dealloc];
}
- (NSString *)windowNibName
{return @MyDocument;
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
if (fileData != nil ) {
[textView replaceCharactersInRange:NSMakeRange(0, 0) withRTF:fileData];
}
//[textStorage addLayoutManager:[textView layoutManager]]; >>>>>ligne en question<<<<
if (loadedText != nil)
{
[textStorage replaceCharactersInRange:NSMakeRange(0,[textStorage length])
withAttributedString:loadedText];
[loadedText release];
loadedText = nil;
}
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
NSRange range = NSMakeRange(0, [[textView textStorage] length]);
return [textView RTFFromRange:range];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
[fileData release];
fileData = data;
[fileData retain];
return fileData != nil;
}
- (IBAction)orderFrontStatisticsPanel:(id)sender
{
[statsPanel orderFront:nil];
}
- (void)statisticsDidChange:(NSNotification *)notification
{
[wordField setIntValue:[textStorage wordCount]];
[charField setIntValue:[textStorage length]];
}
@end
Je suis bloqué vu qu'une des lignes de code m'empêche de charger un fichier mais si je l'enlève, les statistiques restent a 0...
Donc voilà , ci quelqu'un a un ptit coup de main a me filer, je lui serai redevable, merci.
#import <Cocoa/Cocoa.h>
#import "KBWordCountingTextStorage.h"
@interface MyDocument : NSDocument
{
IBOutlet NSPanel *statsPanel;
id IBOutlet textView;
IBOutlet NSTextField *wordField;
IBOutlet NSTextField *charField;
NSData *fileData;
KBWordCountingTextStorage *textStorage;
NSAttributedString *loadedText;
}
- (IBAction)orderFrontStatisticsPanel:(id)sender;
@end
_____________________________________________________________
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
if (self = [super init])
{
textStorage = [[KBWordCountingTextStorage alloc] init];
loadedText = nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statisticsDidChange

name:KBTextStorageStatisticsDidChangeNotification
object:textStorage];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[textStorage release];
[loadedText release]; // Just in case
[super dealloc];
}
- (NSString *)windowNibName
{return @MyDocument;
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
if (fileData != nil ) {
[textView replaceCharactersInRange:NSMakeRange(0, 0) withRTF:fileData];
}
//[textStorage addLayoutManager:[textView layoutManager]]; >>>>>ligne en question<<<<
if (loadedText != nil)
{
[textStorage replaceCharactersInRange:NSMakeRange(0,[textStorage length])
withAttributedString:loadedText];
[loadedText release];
loadedText = nil;
}
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
NSRange range = NSMakeRange(0, [[textView textStorage] length]);
return [textView RTFFromRange:range];
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
[fileData release];
fileData = data;
[fileData retain];
return fileData != nil;
}
- (IBAction)orderFrontStatisticsPanel:(id)sender
{
[statsPanel orderFront:nil];
}
- (void)statisticsDidChange:(NSNotification *)notification
{
[wordField setIntValue:[textStorage wordCount]];
[charField setIntValue:[textStorage length]];
}
@end
Connectez-vous ou Inscrivez-vous pour répondre.
Réponses
Délicat ...
On voit trois NSAttributedString dans ton code : loadedText, [textView textStorage] et textStorage proprement dit. Pourquoi 3 différents ? A priori tu n'en as besoin que d'un, et il est déjà dans le mécanisme du textView, c'est [textView textStorage];
Après tu peux faire une catégorie pour compter les mots et les caractères d'une NSString :