gpt4 book ai didi

ios - swift NSWritingDirection 导致 NSAttributedString 崩溃

转载 作者:行者123 更新时间:2023-11-28 12:05:58 35 4
gpt4 key购买 nike

我有这样的代码:

let myString = "Swift Attributed String"
let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue , NSAttributedStringKey.writingDirection:(NSWritingDirection.rightToLeft.rawValue|NSWritingDirectionFormatType.override.rawValue)] as [NSAttributedStringKey : Any]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)

// set attributed text on a UILabel
self.labelUsername.attributedText = myAttrString

应用程序在上一行崩溃。如果我从属性字典中删除 NSAttributedStringKey.writingDirection,崩溃就会消失。

这是崩溃日志:

2018-03-20 14:38:59.077 Haraj Swift[90519:25834245] -[__NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xb000000000000033
2018-03-20 14:38:59.092 Haraj Swift[90519:25834245] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xb000000000000033'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ef6434b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010e9c521e objc_exception_throw + 48
2 CoreFoundation 0x000000010efd3f34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010eee9c15 ___forwarding___ + 1013
4 CoreFoundation 0x000000010eee9798 _CF_forwarding_prep_0 + 120
5 UIFoundation 0x00000001176e13b3 -[NSATSGlyphStorage setGlyphRange:characterRange:] + 2709
6 UIFoundation 0x00000001176dc5ec -[NSATSTypesetter _ctTypesetter] + 287
7 UIFoundation 0x00000001176e6ef7 -[NSATSLineFragment layoutForStartingGlyphAtIndex:characterIndex:minPosition:maxPosition:lineFragmentRect:] + 95
8 UIFoundation 0x00000001176dd540 -[NSATSTypesetter _layoutLineFragmentStartingWithGlyphAtIndex:characterIndex:atPoint:renderingContext:] + 3240
9 UIFoundation 0x00000001176eeb84 -[NSSingleLineTypesetter createRenderingContextForCharacterRange:typesetterBehavior:usesScreenFonts:hasStrongRight:syncDirection:mirrorsTextAlignment:maximumWidth:] + 408
10 UIFoundation 0x0000000117724167 __NSStringDrawingEngine + 27441
11 UIFoundation 0x00000001177263d4 -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 797
12 UIKit 0x000000010fd1f744 -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 1199
13 UIKit 0x000000010fd1f05b -[UILabel textRectForBounds:limitedToNumberOfLines:] + 68
14 UIKit 0x000000010fd24b7f -[UILabel _intrinsicSizeWithinSize:] + 168
15 UIKit 0x000000010fd24c6c -[UILabel intrinsicContentSize] + 92
16 UIKit 0x00000001104679ed -[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 35
17 UIKit 0x00000001104676ea -[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 494
18 UIKit 0x00000001104714d6 -[UIView(AdditionalLayoutSupport) _updateSystemConstraints] + 66
19 UIKit 0x000000011046ff3e -[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 161
20 UIKit 0x000000011047039d -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 860
21 UIKit 0x00000001104702fa -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 697
22 UIKit 0x00000001104702fa -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 697
23 Foundation 0x000000010e4e1bd0 -[NSISEngine withBehaviors:performModifications:] + 155
24 UIKit 0x00000001104705f2 -[UIView(AdditionalLayoutSupport) _recursiveUpdateConstraintsIfNeededCollectingViews:forSecondPass:] + 118
25 UIKit 0x00000001104702fa -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 697
26 UIKit 0x00000001104702fa -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 697
27 Foundation 0x000000010e4e1bd0 -[NSISEngine withBehaviors:performModifications:] + 155
28 UIKit 0x0000000110470c71 __97-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededPostponeVariableChangeNotifications:]_block_invoke + 91
29 UIKit 0x000000011046fa9b -[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 117
30 UIKit 0x0000000110470757 -[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededPostponeVariableChangeNotifications:] + 181
31 UIKit 0x000000011047184d -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededPostponeVariableChangeNotifications:] + 356
32 UIKit 0x000000010fb4b922 -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
33 UIKit 0x000000010fb5cf50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
34 QuartzCore 0x0000000117054cc4 -[CALayer layoutSublayers] + 146
35 QuartzCore 0x0000000117048788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
36 QuartzCore 0x0000000117048606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
37 QuartzCore 0x0000000116fd6680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
38 QuartzCore 0x0000000117003767 _ZN2CA11Transaction6commitEv + 475
39 QuartzCore 0x00000001170040d7 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
40 CoreFoundation 0x000000010ef08e17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
41 CoreFoundation 0x000000010ef08d87 __CFRunLoopDoObservers + 391
42 CoreFoundation 0x000000010eeedb9e __CFRunLoopRun + 1198
43 CoreFoundation 0x000000010eeed494 CFRunLoopRunSpecific + 420
44 GraphicsServices 0x0000000116f37a6f GSEventRunModal + 161
45 UIKit 0x000000010fa98964 UIApplicationMain + 159
46 Haraj Swift 0x000000010de9e8a7 main + 55
47 libdyld.dylib 0x0000000112fe168d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

尝试使用 NSWritingDirectionarray

    let myString = "Swift Attributed String"

let myAttrString = NSAttributedString(string: myString, attributes: [.foregroundColor : UIColor.blue, .writingDirection: [NSWritingDirection.rightToLeft.rawValue]])

self.label.attributedText = myAttrString

如果您仍然遇到任何问题,请告诉我。

关于ios - swift NSWritingDirection 导致 NSAttributedString 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49383318/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com