Question

Selon la documentation, j'essaie de sous-classer NSTextStorage et de l'utiliser dans la vue texte:

/*
 NSTextStorage is a semi-abstract subclass of NSMutableAttributedString. It
 implements change management (beginEditing/endEditing), verification of
 attributes, delegate handling, and layout management notification. The one
 aspect it does not implement is the actual attributed string storage --- this is
 left up to the subclassers, which need to override the two
 NSMutableAttributedString primitives:

 - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
 - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

*/

J'essaie donc d'utiliser le délégué, pour gérer tous les événements nécessaires avec la même fonctionnalité:

@implementation MyTextStorage

- (id) init {
    self = [super init];

    if (self != nil) {
        storage = [[NSMutableAttributedString alloc] init];
    }

    return self;
}

- (void) dealloc {
    [storage release];
    [super dealloc];
}

- (NSString *) string {
    return [storage string];
}

- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)str {
    [storage replaceCharactersInRange:range withString:str];
}

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range {
    [storage setAttributes:attrs range:range];
}


@end

Peu importe ce que j'utilise en tant que délégué: nSTextStorage ou nsMutableAtTributedString, le résultat est le même:

Une exception non capturée a été soulevée * Nsrunstorage, _nsblockNumberForIndex (): index (18446744073709551615) au-delà des limites du tableau (0) * Terminant l'application due à une exception non revue 'nsRangeException', raison: '*** nsrunstorage, _nsblockNumberForIndex (): Index (18446744073709551615) au-delà des limites du tableau (0)'

Trace de la pile:

0   CoreFoundation                      0x00007fff840cd7b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff885390f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff840cd5d7 +[NSException raise:format:arguments:] + 103
    3   CoreFoundation                      0x00007fff840cd564 +[NSException raise:format:] + 148
    4   AppKit                              0x00007fff86cc6288 _NSBlockNumberForIndex + 86
    5   AppKit                              0x00007fff86cc71d5 -[NSLayoutManager textContainerForGlyphAtIndex:effectiveRange:] + 364
    6   AppKit                              0x00007fff86d1f121 -[NSTextView(NSSharing) didChangeText] + 340
    7   AppKit                              0x00007fff86d44b68 -[NSTextView insertText:replacementRange:] + 2763
    8   CocoaCalculator                     0x0000000100002312 -[CalcTextView insertText:] + 65
    9   CocoaCalculator                     0x00000001000027ac -[CalcTextContainer initWithFrame:] + 1176
    10  AppKit                              0x00007fff86c23d44 -[NSCustomView nibInstantiate] + 646
    11  AppKit                              0x00007fff86b7be17 -[NSIBObjectData instantiateObject:] + 259
    12  AppKit                              0x00007fff86b7b202 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 336
    13  AppKit                              0x00007fff86b7988d loadNib + 226
    14  AppKit                              0x00007fff86b78d9a +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:]

Terminaison commence quand j'essaie d'appeler

[textView insertText:@"123"];

Mais dans la version standard de NSTextStorage, tout fonctionne correctement.

Qu'est-ce que je dois faire pour étendre cette classe?

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top