2012-06-22 3 views
10

जब मैं निम्नलिखित कोड चलाता हूं तो यह अंतिम पंक्ति पर दुर्घटनाग्रस्त हो जाता है। मुझे कोई विचार नहीं है क्यों। यह समारोह awakeFromNib में बुलाया जाता है।NSMutableAttributedString के साथ [NSTextStorage setAttributedString] क्रैश क्यों करता है?

2012-06-22 11:32:22.348 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380 
2012-06-22 11:32:22.349 MSM-Plan[20785:403] An uncaught exception was raised 
2012-06-22 11:32:22.349 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380 
2012-06-22 11:32:22.351 MSM-Plan[20785:403] (
    0 CoreFoundation      0x00007fff8a5bef56 __exceptionPreprocess + 198 
    1 libobjc.A.dylib      0x00007fff8ae19d5e objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190 
    3 CoreFoundation      0x00007fff8a5abe23 ___forwarding___ + 371 
    4 CoreFoundation      0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232 
    5 AppKit        0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249 
    6 AppKit        0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64 
    7 AppKit        0x00007fff8e19b521 -[NSTextStorage processEditing] + 107 
    8 AppKit        0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385 
    9 Foundation       0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328 
    10 AppKit        0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81 
    11 MSM-Plan       0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314 
    12 MSM-Plan       0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701 
    13 CoreFoundation      0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49 
    14 CoreFoundation      0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274 
    15 AppKit        0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245 
    16 AppKit        0x00007fff8e02cf73 loadNib + 322 
    17 AppKit        0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217 
    18 AppKit        0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141 
    19 AppKit        0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364 
    20 AppKit        0x00007fff8e29d06f NSApplicationMain + 398 
    21 MSM-Plan       0x000000010206adf2 main + 34 
    22 MSM-Plan       0x000000010206adc4 start + 52 
) 
2012-06-22 11:32:22.416 MSM-Plan[20785:403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00007fff8a5bef56 __exceptionPreprocess + 198 
    1 libobjc.A.dylib      0x00007fff8ae19d5e objc_exception_throw + 43 
    2 CoreFoundation      0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190 
    3 CoreFoundation      0x00007fff8a5abe23 ___forwarding___ + 371 
    4 CoreFoundation      0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232 
    5 AppKit        0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249 
    6 AppKit        0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64 
    7 AppKit        0x00007fff8e19b521 -[NSTextStorage processEditing] + 107 
    8 AppKit        0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385 
    9 Foundation       0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328 
    10 AppKit        0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81 
    11 MSM-Plan       0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314 
    12 MSM-Plan       0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701 
    13 CoreFoundation      0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49 
    14 CoreFoundation      0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274 
    15 AppKit        0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245 
    16 AppKit        0x00007fff8e02cf73 loadNib + 322 
    17 AppKit        0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 217 
    18 AppKit        0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNameTable:withZone:] + 141 
    19 AppKit        0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364 
    20 AppKit        0x00007fff8e29d06f NSApplicationMain + 398 
    21 MSM-Plan       0x000000010206adf2 main + 34 
    22 MSM-Plan       0x000000010206adc4 start + 52 
) 
terminate called throwing an exception(lldb) 

किसी को भी एक विचार है:

- (void)setMotdText:(NSString *)text 
{ 
    NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName]; 
    NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text]; 

    [attrstr beginEditing]; 

    [attrstr addAttribute:NSFontAttributeName value:boldFontName range:NSMakeRange(0, 16)]; 

    [attrstr endEditing]; 

    [self.motdTextView.textStorage setAttributedString:attrstr]; 
} 

मैं इस क्रैश लॉग मिल सकता है? अगर मैं इसे एनएसएट्रिब्यूटेड स्ट्रिंग-ऑब्जेक्ट के साथ बुलाता हूं तो इसमें कोई त्रुटि नहीं है। ,

NSFont *boldFont = [NSFont boldSystemFontOfSize:12]; 

अब यह काम करता है क्योंकि मूल्य विशेषता एक NSFont-वस्तु और फ़ॉन्ट नाम के साथ नहीं एक NSString की आवश्यकता है:

उत्तर

18

मैं पहली पंक्ति को बदलना पड़ा।

यह काम कर कोड है:

- (void)setMotdText:(NSString *)text 
{ 
    NSFont *boldFont = [NSFont boldSystemFontOfSize:12]; 
    NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text]; 

    [attrstr beginEditing]; 

    [attrstr addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(0, 16)]; 

    [attrstr endEditing]; 

    [self.motdTextView.textStorage setAttributedString:attrstr]; 
} 
+0

अरे, मैं वही गलती :) बनाया है। "NSFontAttributeName" फ़ॉन्ट का नाम-विशेषता नहीं है; यह फ़ॉन्ट-विशेषता का नाम है। ऐप्पल की नई नामकरण योजना इन बगों से बचाती है (उदा। इस एपीआई को इसके बजाय "kFontAttribute" जैसे कुछ का उपयोग करना चाहिए), लेकिन मुझे लगता है कि हम पुराने बुरे एपीआई नामों के साथ हमेशा के लिए अटक गए हैं :( – Adam