[英]Why does this call to NSMutableAttributedString addAttributes crash?
I'm getting a frequent crash on calling addAttributes
out in the field that I can't reproduce (EXC_BREAKPOINT). 我在无法重现的字段中调用
addAttributes
时经常崩溃(EXC_BREAKPOINT)。 Have added checks on the NSRange I'm passing in and surprisingly it still crashes... any ideas very gratefully received! 添加了对我传入的NSRange的检查,令人惊讶的是它仍然崩溃......任何想法都非常感激!
let boldAttributes : [NSAttributedStringKey: Any] = [.font: cellStyle.boldFont()]
if (nsrange.location >= 0 && nsrange.length > 0 && nsrange.location + nsrange.length <= myAttributedString.length) {
myAttributedString.addAttributes(boldAttributes, range: nsrange)
}
Crash log added by request: 根据请求添加崩溃日志:
#0. Crashed: com.apple.main-thread
0 MyApp 0x10054ae38 specialized UIAccessorizedTextField.accessoryAttributedString(IndexPath, cellStyle : UIAccessorizedTextFieldCellStyle) -> NSAttributedString (UIAccessorizedTextField.swift:322)
1 MyApp 0x10054b104 specialized UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift:345)
2 MyApp 0x10054860c @objc UIAccessorizedTextField.collectionView(UICollectionView, cellForItemAt : IndexPath) -> UICollectionViewCell (UIAccessorizedTextField.swift)
3 UIKit 0x18b229f3c -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 356
4 UIKit 0x18bb90b68 -[UICollectionView _prefetchItemsForVelocity:maxItemsToPrefetch:invalidateCandidatesOnDirectionChanges:] + 508
5 UIKit 0x18b2088b4 -[UICollectionView layoutSubviews] + 760
6 UIKit 0x18b0d76f4 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1420
7 QuartzCore 0x18564dfec -[CALayer layoutSublayers] + 184
8 QuartzCore 0x18565217c CA::Layer::layout_if_needed(CA::Transaction*) + 324
9 QuartzCore 0x1855be830 CA::Context::commit_transaction(CA::Transaction*) + 320
10 QuartzCore 0x1855e6364 CA::Transaction::commit() + 580
11 QuartzCore 0x1855e71e4 CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92
12 CoreFoundation 0x18146e910 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
13 CoreFoundation 0x18146c238 __CFRunLoopDoObservers + 412
14 CoreFoundation 0x18146c884 __CFRunLoopRun + 1436
15 CoreFoundation 0x18138cda8 CFRunLoopRunSpecific + 552
16 GraphicsServices 0x183371020 GSEventRunModal + 100
17 UIKit 0x18b3a9758 UIApplicationMain + 236
18 MyApp 0x1003f2830 main (main.m:16)
19 libdyld.dylib 0x180e1dfc0 start + 4
As the crash is not easily reproducible, it should depend on the contents of myAttributedString
, or nsrange
: The surrounding code provides values that pass your checks, but still crash. 由于崩溃不易重现,它应该取决于
myAttributedString
或nsrange
的内容:周围的代码提供了通过检查的值,但仍然崩溃。 There are two ways I am aware of how to achieve that. 有两种方法我知道如何实现这一目标。
The first is by passing an NSRange
that has location NSNotFound
and length > 0
. 第一种方法是传递一个位置为
NSNotFound
且length > 0
的NSRange
。 Example: 例:
let myAttributedString = NSMutableAttributedString(string: "Hello")
let nsrange = NSRange(location: NSNotFound, length: 1)
The second is a crash in older iOS versions when adding attributes to a subset of an emoji. 第二个是在向表情符号的子集添加属性时旧iOS版本中的崩溃。 If you only see crashes in older iOS versions, it should be a scenario like this one:
如果你只看到旧iOS版本中的崩溃,它应该是这样的场景:
let myAttributedString = NSMutableAttributedString(string: "👨👩👧 is a family")
let nsrange = NSRange(location: 1, length: 2)
So if you see the crash for all iOS versions, you can check for nsrange != NSNotFound
first to fix the crash. 因此,如果您看到所有iOS版本的崩溃,您可以首先检查
nsrange != NSNotFound
来修复崩溃。 If you see the crash only for older iOS versions, then check the surrounding code for places where you could calculate a wrong range. 如果您只看到旧版iOS的崩溃,请检查周围的代码,找出可以计算错误范围的地方。 This typically happens when mixing string info from Swift with string info from Objective-C's Foundation.
当将来自Swift的字符串信息与来自Objective-C的Foundation的字符串信息混合时,通常会发生这种情况。 For instance,
"👨👩👧".count
is 1, whereas ("👨👩👧" as NSString).length
is 8. 例如,
"👨👩👧".count
为1,而("👨👩👧" as NSString).length
为8。
First of all need to be sure that cellStyle.boldFont()
return correct value. 首先需要确保
cellStyle.boldFont()
返回正确的值。 Try to use UIFont.boldSystemFont(ofSize: 15)
to be sure that you have correct value in boldAttributes
尝试使用
UIFont.boldSystemFont(ofSize: 15)
以确保您在boldAttributes
具有正确的值
let boldAttributes : [NSAttributedStringKey: Any] = [.font: UIFont.boldSystemFont(ofSize: 15)]
if (nsrange.location >= 0 && nsrange.length > 0 && nsrange.location + nsrange.length <= myAttributedString.length) {
myAttributedString.addAttributes(boldAttributes, range: nsrange)
}
After that, double re-check that myAttributedString
is NSMutableAttributedString
class using runtime debugger. 之后,使用运行时调试器重新检查
myAttributedString
是否为NSMutableAttributedString
类。 Maybe it is immutable NSAttributedString
and you trying to add your attributes to it... 也许它是不可变的
NSAttributedString
,你试图添加你的属性...
After that, if you still have crash, need to check nsrange
value. 之后,如果仍然崩溃,需要检查
nsrange
值。 Maybe you have nil
or some incorrect value there 也许你那里
nil
或一些不正确的价值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.