Aide NSDocument petit logiciel test

MekolinMekolin Membre
23:48 modifié dans Vos applications #1
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


Réponses

  • Philippe49Philippe49 Membre
    mars 2009 modifié #2
    Tu sous-classes NSTextStorage ?
    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 :

    @interface&nbsp; NSString(KBWordCounting) <br />-(NSUInteger) wordsCount;<br />-(NSUInteger) charactersCount;<br />@end<br /><br />@implementation NSMutableAttributedString(KBWordCounting) <br />-(NSUInteger) wordsCount{<br />.......<br />}<br />-(NSUInteger) charactersCount{<br />...........<br />}<br />@end
    
Connectez-vous ou Inscrivez-vous pour répondre.