![](/img/trans.png)
[英]iOS: NSMutableAttributedString.AddAttributes not rendered. (Xamarin)
[英]Why does this call to NSMutableAttributedString addAttributes crash?
我在無法重現的字段中調用addAttributes
時經常崩潰(EXC_BREAKPOINT)。 添加了對我傳入的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)
}
根據請求添加崩潰日志:
#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
由於崩潰不易重現,它應該取決於myAttributedString
或nsrange
的內容:周圍的代碼提供了通過檢查的值,但仍然崩潰。 有兩種方法我知道如何實現這一目標。
第一種方法是傳遞一個位置為NSNotFound
且length > 0
的NSRange
。 例:
let myAttributedString = NSMutableAttributedString(string: "Hello")
let nsrange = NSRange(location: NSNotFound, length: 1)
第二個是在向表情符號的子集添加屬性時舊iOS版本中的崩潰。 如果你只看到舊iOS版本中的崩潰,它應該是這樣的場景:
let myAttributedString = NSMutableAttributedString(string: "👨👩👧 is a family")
let nsrange = NSRange(location: 1, length: 2)
因此,如果您看到所有iOS版本的崩潰,您可以首先檢查nsrange != NSNotFound
來修復崩潰。 如果您只看到舊版iOS的崩潰,請檢查周圍的代碼,找出可以計算錯誤范圍的地方。 當將來自Swift的字符串信息與來自Objective-C的Foundation的字符串信息混合時,通常會發生這種情況。 例如, "👨👩👧".count
為1,而("👨👩👧" as NSString).length
為8。
首先需要確保cellStyle.boldFont()
返回正確的值。 嘗試使用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)
}
之后,使用運行時調試器重新檢查myAttributedString
是否為NSMutableAttributedString
類。 也許它是不可變的NSAttributedString
,你試圖添加你的屬性...
之后,如果仍然崩潰,需要檢查nsrange
值。 也許你那里nil
或一些不正確的價值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.