objective-c – 为什么[NSTextStorage setAttributedString]与NSMutableAttributedString崩溃?

前端之家收集整理的这篇文章主要介绍了objective-c – 为什么[NSTextStorage setAttributedString]与NSMutableAttributedString崩溃?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我运行以下代码时,它会在最后一行崩溃.我不知道为什么这个函数在awakeFromNib中调用.
  1. - (void)setMotdText:(NSString *)text
  2. {
  3. NSString *boldFontName = [[NSFont boldSystemFontOfSize:12] fontName];
  4. NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
  5.  
  6. [attrstr beginEditing];
  7.  
  8. [attrstr addAttribute:NSFontAttributeName value:boldFontName range:NSMakeRange(0,16)];
  9.  
  10. [attrstr endEditing];
  11.  
  12. [self.motdTextView.textStorage setAttributedString:attrstr];
  13. }

我得到这个崩溃日志:

  1. 2012-06-22 11:32:22.348 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
  2. 2012-06-22 11:32:22.349 MSM-Plan[20785:403] An uncaught exception was raised
  3. 2012-06-22 11:32:22.349 MSM-Plan[20785:403] -[__NSCFConstantString _isDefaultFace]: unrecognized selector sent to instance 0x7fff79b9f380
  4. 2012-06-22 11:32:22.351 MSM-Plan[20785:403] (
  5. 0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
  6. 1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
  7. 2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
  8. 3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
  9. 4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
  10. 5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
  11. 6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
  12. 7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
  13. 8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
  14. 9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
  15. 10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
  16. 11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
  17. 12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
  18. 13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
  19. 14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
  20. 15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
  21. 16 AppKit 0x00007fff8e02cf73 loadNib + 322
  22. 17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:naMetable:withZone:ownerBundle:] + 217
  23. 18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNaMetable:withZone:] + 141
  24. 19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
  25. 20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
  26. 21 MSM-Plan 0x000000010206adf2 main + 34
  27. 22 MSM-Plan 0x000000010206adc4 start + 52
  28. )
  29. 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'
  30. *** First throw call stack:
  31. (
  32. 0 CoreFoundation 0x00007fff8a5bef56 __exceptionPreprocess + 198
  33. 1 libobjc.A.dylib 0x00007fff8ae19d5e objc_exception_throw + 43
  34. 2 CoreFoundation 0x00007fff8a64b1be -[NSObject doesNotRecognizeSelector:] + 190
  35. 3 CoreFoundation 0x00007fff8a5abe23 ___forwarding___ + 371
  36. 4 CoreFoundation 0x00007fff8a5abc38 _CF_forwarding_prep_0 + 232
  37. 5 AppKit 0x00007fff8e0b060c -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixFontAttributeInRange:] + 1249
  38. 6 AppKit 0x00007fff8e0afeb7 -[NSMutableAttributedString(NSMutableAttributedStringKitAdditions) fixAttributesInRange:] + 64
  39. 7 AppKit 0x00007fff8e19b521 -[NSTextStorage processEditing] + 107
  40. 8 AppKit 0x00007fff8e0c2564 -[NSTextStorage edited:range:changeInLength:] + 385
  41. 9 Foundation 0x00007fff8d6f4497 -[NSConcreteMutableAttributedString replaceCharactersInRange:withAttributedString:] + 328
  42. 10 AppKit 0x00007fff8e1dfd19 -[NSConcreteTextStorage replaceCharactersInRange:withAttributedString:] + 81
  43. 11 MSM-Plan 0x000000010206af5a -[MSM_PlanAppDelegate setMotdText:] + 314
  44. 12 MSM-Plan 0x000000010206c455 -[MSM_PlanAppDelegate awakeFromNib] + 1701
  45. 13 CoreFoundation 0x00007fff8a5b5fb1 -[NSObject performSelector:] + 49
  46. 14 CoreFoundation 0x00007fff8a5b5f32 -[NSSet makeObjectsPerformSelector:] + 274
  47. 15 AppKit 0x00007fff8e0369ff -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1245
  48. 16 AppKit 0x00007fff8e02cf73 loadNib + 322
  49. 17 AppKit 0x00007fff8e02c470 +[NSBundle(NSNibLoading) _loadNibFile:naMetable:withZone:ownerBundle:] + 217
  50. 18 AppKit 0x00007fff8e02c38b +[NSBundle(NSNibLoading) loadNibFile:externalNaMetable:withZone:] + 141
  51. 19 AppKit 0x00007fff8e02c2ce +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 364
  52. 20 AppKit 0x00007fff8e29d06f NSApplicationMain + 398
  53. 21 MSM-Plan 0x000000010206adf2 main + 34
  54. 22 MSM-Plan 0x000000010206adc4 start + 52
  55. )
  56. terminate called throwing an exception(lldb)

有人有想法吗?如果我用NSAttributedString-Object调用它,那里没有错误.

解决方法

我不得不将第一行改为:
  1. NSFont *boldFont = [NSFont boldSystemFontOfSize:12];

现在它的工作原理是因为value属性需要一个NSFont-Object,而不是带有字体名称的NSString.

这是工作代码

  1. - (void)setMotdText:(NSString *)text
  2. {
  3. NSFont *boldFont = [NSFont boldSystemFontOfSize:12];
  4. NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc] initWithString:text];
  5.  
  6. [attrstr beginEditing];
  7.  
  8. [attrstr addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(0,16)];
  9.  
  10. [attrstr endEditing];
  11.  
  12. [self.motdTextView.textStorage setAttributedString:attrstr];
  13. }

猜你在找的C&C++相关文章