Post

Replies

Boosts

Views

Activity

Unexpected crash when typing text into NSTextView
I'm getting the below stacktrace when running myApp and entering some text in a NSTextView, but none of the lines in it correspond to my code. Probably I did something wrong in my code that AppKit should validate with asserts that tell me what I'm doing wrong.2019-12-10 17:50:54.724652+0100 myApp[85138:2956324] <NSATSTypesetter: 0x600003705380>: Exception NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds raised during typesetting layout manager <NSLayoutManager: 0x103003c40> 1 containers, text backing has 45 characters selected character range {10, 0} affinity: upstream granularity: character marked character range {45, 0} Currently holding 46 glyphs. Glyph tree contents: 46 characters, 46 glyphs, 3 nodes, 192 node bytes, 128 storage bytes, 320 total bytes, 6.96 bytes per character, 6.96 bytes per glyph Layout tree contents: 45 characters, 46 glyphs, 1 laid glyphs, 1 laid line fragments, 3 nodes, 192 node bytes, 1712 storage bytes, 1904 total bytes, 42.31 bytes per character, 41.39 bytes per glyph, 1.00 laid glyphs per laid line fragment, 1904.00 bytes per laid line fragment, glyph range {0 46}. Ignoring...2019-12-10 17:50:54.724867+0100 myApp[85138:2956324] [General] An uncaught exception was raised2019-12-10 17:50:54.724930+0100 myApp[85138:2956324] [General] *** -[NSBigMutableString characterAtIndex:]: Index 18446744073709551615 out of bounds; string length 452019-12-10 17:50:54.727792+0100 myApp[85138:2956324] [General] ( 0 CoreFoundation 0x00007fff2d27df53 __exceptionPreprocess + 250 1 libobjc.A.dylib 0x00007fff63343835 objc_exception_throw + 48 2 Foundation 0x00007fff2f8e213d -[NSBigMutableString characterAtIndex:] + 250 3 UIFoundation 0x00007fff5de0e459 -[NSLayoutManager(NSPrivate) _insertionPointHelperForGlyphAtIndex:] + 1808 4 UIFoundation 0x00007fff5de0da90 _insertionPointHelperForCharacterAtIndex + 1110 5 UIFoundation 0x00007fff5de04fd4 -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:rectArray:rectArrayCapacity:] + 4432 6 UIFoundation 0x00007fff5df56f02 -[NSLayoutManager(NSPrivate) _rectArrayForRange:withinSelectionRange:rangeIsCharRange:singleRectOnly:fullLineRectsOnly:inTextContainer:rectCount:rangeWithinContainer:glyphsDrawOutsideLines:] + 58 7 UIFoundation 0x00007fff5de12344 -[NSLayoutManager rectArrayForCharacterRange:withinSelectedCharacterRange:inTextContainer:rectCount:] + 35 8 AppKit 0x00007fff2a43dd93 -[NSTextView(NSSharing) updateInsertionPointStateAndRestartTimer:] + 1450 9 AppKit 0x00007fff2a4a9400 -[NSTextView(NSSharing) setSelectedRanges:affinity:stillSelecting:] + 1630 10 UIFoundation 0x00007fff5ddb3e66 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 397 11 UIFoundation 0x00007fff5dda8ce9 -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 171 12 UIFoundation 0x00007fff5dda440d -[NSTextStorage processEditing] + 373 13 UIFoundation 0x00007fff5ddb3bd1 -[NSTextStorage endEditing] + 134 14 AppKit 0x00007fff2abbeaeb -[NSTextView _insertText:replacementRange:] + 2933 15 AppKit 0x00007fff2a6b8132 -[NSTextInputContext(NSInputContext_WithCompletion) insertText:replacementRange:completionHandler:] + 184 16 AppKit 0x00007fff2ad992d2 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2.326 + 102 17 AppKit 0x00007fff2a6b8069 -[NSTextInputContext do_HandleTSMEvent_insertFixLenTextLoop:whileCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 105 18 AppKit 0x00007fff2ad97410 -[NSTextInputContext tryHandleTSMEvent_insertFixLenText_withContext:dispatchCondition:setupForDispatch:nestedWorkaroundCondition:nestedWorkaroundDispatchWork:loopCondition:dispatchWorkEach:afterEachInsertText:continuation:] + 310 19 AppKit 0x00007fff2ad98bd4 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke.280 + 3027 20 AppKit 0x00007fff2a6b7cec __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2 + 74 21 AppKit 0x00007fff2a6b7c6d -[NSTextInputContext tryHandleTSMEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 87 22 AppKit 0x00007fff2a6b7474 -[NSTextInputContext handleTSMEvent:completionHandler:] + 1734 23 AppKit 0x00007fff2a6b6d35 _NSTSMEventHandler + 299 24 HIToolbox 0x00007fff2bd51701 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1419 25 HIToolbox 0x00007fff2bd50ad0 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 338 26 HIToolbox 0x00007fff2bd50973 SendEventToEventTargetWithOptions + 45 27 HIToolbox 0x00007fff2bdb757b SendTSMEvent_WithCompletionHandler + 381 28 HIToolbox 0x00007fff2bdb79d9 __SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler_block_invoke + 387 29 HIToolbox 0x00007fff2bdb7831 __SendFilterTextEvent_WithCompletionHandler_block_invoke + 182 30 HIToolbox 0x00007fff2bdb75ca SendTSMEvent_WithCompletionHandler + 460 31 HIToolbox 0x00007fff2bdb73d1 SendFilterTextEvent_WithCompletionHandler + 225 32 HIToolbox 0x00007fff2bdb70a0 SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler + 277 33 HIToolbox 0x00007fff2bdb6e4d __utDeliverTSMEvent_WithCompletionHandler_block_invoke_2 + 281 34 HIToolbox 0x00007fff2bdb6cad __utDeliverTSMEvent_WithCompletionHandler_block_invoke + 355 35 HIToolbox 0x00007fff2bdb6acc TSMKeyEvent_WithCompletionHandler + 598 36 HIToolbox 0x00007fff2bdb685b __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_4 + 250 37 HIToolbox 0x00007fff2bdb668a __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_3 + 257 38 HIToolbox 0x00007fff2bdb63cf __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_2 + 282 39 HIToolbox 0x00007fff2bdb6133 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke + 274 40 HIToolbox 0x00007fff2bdb5761 TSMProcessRawKeyEventWithOptionsAndCompletionHandler + 3403 41 AppKit 0x00007fff2ad9d8a6 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke_3.802 + 110 42 AppKit 0x00007fff2ad9ced9 __204-[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:]_block_invoke.759 + 115 43 AppKit 0x00007fff2ad9cdca -[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:] + 245 44 AppKit 0x00007fff2ad9d47e -[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:] + 1261 45 AppKit 0x00007fff2ad9cc89 -[NSTextInputContext _handleEvent:allowingSyntheticEvent:] + 105 46 AppKit 0x00007fff2a6b5b73 -[NSView interpretKeyEvents:] + 209 47 AppKit 0x00007fff2a6b59b6 -[NSTextView keyDown:] + 699 48 AppKit 0x00007fff2a51fdec -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 6512 49 AppKit 0x00007fff2a51e23d -[NSWindow(NSEventRouting) sendEvent:] + 349 50 AppKit 0x00007fff2a3e2daf -[NSApplication(NSEvent) sendEvent:] + 2739 51 AppKit 0x00007fff2a3d1da7 -[NSApplication run] + 707 52 AppKit 0x00007fff2a3c395d NSApplicationMain + 777 53 myApp 0x00000001000c7ead main + 13 54 libdyld.dylib 0x00007fff646a62e5 start + 1 55 ??? 0x0000000000000003 0x0 + 3)
5
0
1.7k
Dec ’19
App hangs when two threads call URL.appendingPathComponent()
A customer reported that my app hangs at some point and he sent me a process analysis taken from Activity Monitor. To me it looks like two threads are calling URL.appendingPathComponent() at the same time and this causes a deadlock. Anyone knows how to avoid this and if this is a problem with macOS?Call graph: 2662 Thread_13677 DispatchQueue_1: com.apple.main-thread (serial) + 2662 start (in libdyld.dylib) + 1 [0x7fff632ce7fd] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x1bb9 [0x1086d1bb9] + 2662 NSApplicationMain (in AppKit) + 777 [0x7fff28dfc775] + 2662 -[NSApplication run] (in AppKit) + 658 [0x7fff28e2a3ae] + 2662 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] (in AppKit) + 1352 [0x7fff28e38690] + 2662 _DPSNextEvent (in AppKit) + 990 [0x7fff28e39eb4] + 2662 _BlockUntilNextEventMatchingListInModeWithFilter (in HIToolbox) + 64 [0x7fff2a7c8127] + 2662 ReceiveNextEventCommon (in HIToolbox) + 600 [0x7fff2a7c839d] + 2662 RunCurrentEventLoopInMode (in HIToolbox) + 292 [0x7fff2a7c865d] + 2662 CFRunLoopRunSpecific (in CoreFoundation) + 499 [0x7fff2bc71bd3] + 2662 __CFRunLoopRun (in CoreFoundation) + 2370 [0x7fff2bc72b8a] + 2662 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ (in CoreFoundation) + 9 [0x7fff2bcb2e00] + 2662 _dispatch_main_queue_callback_4CF (in libdispatch.dylib) + 1105 [0x7fff63280bc4] + 2662 _dispatch_client_callout (in libdispatch.dylib) + 8 [0x7fff6327550e] + 2662 _dispatch_call_block_and_release (in libdispatch.dylib) + 12 [0x7fff63274583] + 2662 ??? (in myApp) load address 0x1086d0000 + 0xa3169 [0x108773169] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x25f1e [0x1086f5f1e] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x26201 [0x1086f6201] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x87eb7 [0x108757eb7] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x81da1 [0x108751da1] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x6d18 [0x1086d6d18] + 2662 URL.appendingPathComponent(_:) (in libswiftFoundation.dylib) + 61 [0x7fff62e1e3ad] + 2662 -[NSURL(NSURLPathUtilities) URLByAppendingPathComponent:] (in Foundation) + 217 [0x7fff2e2e843a] + 2662 -[NSURL getResourceValue:forKey:error:] (in CoreFoundation) + 110 [0x7fff2bc5d4ff] + 2662 CFURLCopyResourcePropertyForKey (in CoreFoundation) + 119 [0x7fff2bc46958] + 2662 _FSURLCopyResourcePropertyForKeyInternal(__CFURL const*, __CFString const*, void*, void*, __CFError**, unsigned char) (in CoreServicesInternal) + 221 [0x7fff4472feba] + 2662 prepareValuesForBitmap(__CFURL const*, __FileCache*, _FilePropertyBitmap*, __CFError**) (in CoreServicesInternal) + 363 [0x7fff447337e4] + 2662 corePropertyProviderPrepareValues(__CFURL const*, __FileCache*, __CFString const* const*, void const**, long, void const*, __CFError**) (in CoreServicesInternal) + 631 [0x7fff44733ac4] + 2662 __getattrlist (in libsystem_kernel.dylib) + 10 [0x7fff6340fad2] 2662 Thread_13693 DispatchQueue_6: com.apple.root.background-qos (concurrent) + 2662 start_wqthread (in libsystem_pthread.dylib) + 15 [0x7fff634ce827] + 2662 _pthread_wqthread (in libsystem_pthread.dylib) + 220 [0x7fff634cf6b6] + 2662 _dispatch_worker_thread2 (in libdispatch.dylib) + 92 [0x7fff63283f22] + 2662 _dispatch_root_queue_drain (in libdispatch.dylib) + 663 [0x7fff63283933] + 2662 _dispatch_client_callout (in libdispatch.dylib) + 8 [0x7fff6327550e] + 2662 _dispatch_call_block_and_release (in libdispatch.dylib) + 12 [0x7fff63274583] + 2662 ??? (in myApp) load address 0x1086d0000 + 0xa3169 [0x108773169] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x446aa [0x1087146aa] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x46533 [0x108716533] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3d9 [0x10871a3d9] + 2662 ??? (in myApp) load address 0x1086d0000 + 0x4a3be [0x10871a3be] + 2662 URL.appendingPathComponent(_:) (in libswiftFoundation.dylib) + 61 [0x7fff62e1e3ad] + 2662 -[NSURL(NSURLPathUtilities) URLByAppendingPathComponent:] (in Foundation) + 217 [0x7fff2e2e843a] + 2662 -[NSURL getResourceValue:forKey:error:] (in CoreFoundation) + 110 [0x7fff2bc5d4ff] + 2662 CFURLCopyResourcePropertyForKey (in CoreFoundation) + 119 [0x7fff2bc46958] + 2662 _FSURLCopyResourcePropertyForKeyInternal(__CFURL const*, __CFString const*, void*, void*, __CFError**, unsigned char) (in CoreServicesInternal) + 221 [0x7fff4472feba] + 2662 prepareValuesForBitmap(__CFURL const*, __FileCache*, _FilePropertyBitmap*, __CFError**) (in CoreServicesInternal) + 363 [0x7fff447337e4] + 2662 corePropertyProviderPrepareValues(__CFURL const*, __FileCache*, __CFString const* const*, void const**, long, void const*, __CFError**) (in CoreServicesInternal) + 631 [0x7fff44733ac4] + 2662 __getattrlist (in libsystem_kernel.dylib) + 10 [0x7fff6340fad2] 2662 Thread_13846: com.apple.NSEventThread + 2662 thread_start (in libsystem_pthread.dylib) + 15 [0x7fff634ce83b] + 2662 _pthread_start (in libsystem_pthread.dylib) + 148 [0x7fff634d2e65] + 2662 _NSEventThread (in AppKit) + 132 [0x7fff28fdca72] + 2662 CFRunLoopRunSpecific (in CoreFoundation) + 499 [0x7fff2bc71bd3] + 2662 __CFRunLoopRun (in CoreFoundation) + 1695 [0x7fff2bc728e7] + 2662 __CFRunLoopServiceMachPort (in CoreFoundation) + 322 [0x7fff2bc73d0b] + 2662 mach_msg (in libsystem_kernel.dylib) + 60 [0x7fff6340f5d0] + 2662 mach_msg_trap (in libsystem_kernel.dylib) + 10 [0x7fff6340f25a] 2662 Thread_59695 DispatchQueue_158: com.apple.CFVolumeObserver.0x600003dbf8a0 (serial) + 2662 start_wqthread (in libsystem_pthread.dylib) + 15 [0x7fff634ce827] + 2662 _pthread_wqthread (in libsystem_pthread.dylib) + 290 [0x7fff634cf6fc] + 2662 _dispatch_workloop_worker_thread (in libdispatch.dylib) + 598 [0x7fff63284a9e] + 2662 _dispatch_lane_invoke (in libdispatch.dylib) + 363 [0x7fff6327b452] + 2662 _dispatch_lane_serial_drain (in libdispatch.dylib) + 263 [0x7fff6327a980] + 2662 _dispatch_source_invoke (in libdispatch.dylib) + 2084 [0x7fff632873c4] + 2662 _dispatch_continuation_pop (in libdispatch.dylib) + 414 [0x7fff632776c0] + 2662 _dispatch_client_callout (in libdispatch.dylib) + 8 [0x7fff6327550e] + 2662 __DASessionSetDispatchQueue_block_invoke_2 (in DiskArbitration) + 56 [0x7fff2df7de66] + 2662 _DASessionCallback (in DiskArbitration) + 259 [0x7fff2df7df70] + 2662 _DADispatchCallback (in DiskArbitration) + 198 [0x7fff2df7e254] + 2662 _VolumeObserverDiskUnmountApprovalCallback (in CoreFoundation) + 159 [0x7fff2bd5d738] + 2662 __createVolumeObserverSubsystem_block_invoke (in AppKit) + 992 [0x7fff291110ea] + 2662 _dispatch_semaphore_wait_slow (in libdispatch.dylib) + 98 [0x7fff63275e6a] + 2662 _dispatch_sema4_wait (in libdispatch.dylib) + 16 [0x7fff6327599d] + 2662 semaphore_wait_trap (in libsystem_kernel.dylib) + 10 [0x7fff6340f296] 2662 Thread_233483 2662 start_wqthread (in libsystem_pthread.dylib) + 15 [0x7fff634ce827] 2662 _pthread_wqthread (in libsystem_pthread.dylib) + 390 [0x7fff634cf760] 2662 __workq_kernreturn (in libsystem_kernel.dylib) + 10 [0x7fff6341092e]
1
0
1.6k
Dec ’19
Trying to print NSAttributedString via many UITextViews causes crash
My Mac app uses many `NSTextView`s just fine for printing a long string, but trying to do the same on iOS with many `UITextView`s and associated `UIViewPrintFormatter`s just causes a crash.*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: ( 0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23d03ab1 _CFThrowFormattedException + 194 3 CoreFoundation 0x00007fff23b83bf9 -[__NSArrayM objectAtIndex:] + 169 4 UIKitCore 0x00007fff48029503 -[UITextViewPrintFormatter rectForPageAtIndex:] + 86 5 UIKitCore 0x00007fff4806bc79 __57-[UIPrintPageRenderer drawPrintFormatter:forPageAtIndex:]_block_invoke + 41 6 UIKitCore 0x00007fff4806bcf9 __57-[UIPrintPageRenderer drawPrintFormatter:forPageAtIndex:]_block_invoke.43 + 29 7 libdispatch.dylib 0x000000010e4f6d48 _dispatch_client_callout + 8 8 libdispatch.dylib 0x000000010e505b24 _dispatch_async_and_wait_invoke + 175 9 libdispatch.dylib 0x000000010e4f6d48 _dispatch_client_callout + 8 10 libdispatch.dylib 0x000000010e504de6 _dispatch_main_queue_callback_4CF + 1500 11 CoreFoundation 0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 12 CoreFoundation 0x00007fff23bceca9 __CFRunLoopRun + 2329 13 CoreFoundation 0x00007fff23bce066 CFRunLoopRunSpecific + 438 14 GraphicsServices 0x00007fff384c0bb0 GSEventRunModal + 65 15 UIKitCore 0x00007fff48092d4d UIApplicationMain + 1621 16 myApp 0x000000010e1ca61b main + 75 17 libdyld.dylib 0x00007fff5227ec25 start + 1 18 ??? 0x0000000000000001 0x0 + 1 )Here's the code:class ViewController: UIViewController { override func viewDidAppear(_ animated: Bool) { let printController = UIPrintInteractionController.shared let printPageRenderer = PrintPageRenderer() printController.printPageRenderer = printPageRenderer printController.present(animated: true) } } class PrintPageRenderer: UIPrintPageRenderer { let layoutManager = NSLayoutManager() let textStorage = NSTextStorage(string: "fhdjksalhfj dskla fjf") var textViews = [UITextView]() override func prepare(forDrawingPages range: NSRange) { DispatchQueue.main.sync { for (i, textView) in textViews.enumerated() { let printFormatter = textView.viewPrintFormatter() addPrintFormatter(printFormatter, startingAtPageAt: i) } } } override var numberOfPages: Int { textStorage.addLayoutManager(layoutManager) let size = CGFloat(50) for _ in 0..<2 { let textContainer = NSTextContainer(size: CGSize(width: size, height: size)) layoutManager.addTextContainer(textContainer) let textView = UITextView(frame: CGRect(x: 0, y: 0, width: size, height: size), textContainer: textContainer) textViews.append(textView) } return textViews.count } }My goal is printing a document from an attributed string and showing a custom header and footer on each page.
2
0
719
Jan ’20
Trying to print NSAttributedString via many UITextViews causes crash
My Mac app uses many NSTextViews just fine for printing a long string, but trying to do the same on iOS with many UITextViews and associated UIViewPrintFormatters just causes a crash.*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: ( 0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23d03ab1 _CFThrowFormattedException + 194 3 CoreFoundation 0x00007fff23b83bf9 -[__NSArrayM objectAtIndex:] + 169 4 UIKitCore 0x00007fff48029503 -[UITextViewPrintFormatter rectForPageAtIndex:] + 86 5 UIKitCore 0x00007fff4806bc79 __57-[UIPrintPageRenderer drawPrintFormatter:forPageAtIndex:]_block_invoke + 41 6 UIKitCore 0x00007fff4806bcf9 __57-[UIPrintPageRenderer drawPrintFormatter:forPageAtIndex:]_block_invoke.43 + 29 7 libdispatch.dylib 0x000000010e4f6d48 _dispatch_client_callout + 8 8 libdispatch.dylib 0x000000010e505b24 _dispatch_async_and_wait_invoke + 175 9 libdispatch.dylib 0x000000010e4f6d48 _dispatch_client_callout + 8 10 libdispatch.dylib 0x000000010e504de6 _dispatch_main_queue_callback_4CF + 1500 11 CoreFoundation 0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 12 CoreFoundation 0x00007fff23bceca9 __CFRunLoopRun + 2329 13 CoreFoundation 0x00007fff23bce066 CFRunLoopRunSpecific + 438 14 GraphicsServices 0x00007fff384c0bb0 GSEventRunModal + 65 15 UIKitCore 0x00007fff48092d4d UIApplicationMain + 1621 16 myApp 0x000000010e1ca61b main + 75 17 libdyld.dylib 0x00007fff5227ec25 start + 1 18 ??? 0x0000000000000001 0x0 + 1 )Here's the code:class ViewController: UIViewController { override func viewDidAppear(_ animated: Bool) { let printController = UIPrintInteractionController.shared let printPageRenderer = PrintPageRenderer() printController.printPageRenderer = printPageRenderer printController.present(animated: true) } } class PrintPageRenderer: UIPrintPageRenderer { let layoutManager = NSLayoutManager() let textStorage = NSTextStorage(string: "fhdjksalhfj dskla fjf") var textViews = [UITextView]() override func prepare(forDrawingPages range: NSRange) { DispatchQueue.main.sync { for (i, textView) in textViews.enumerated() { let printFormatter = textView.viewPrintFormatter() addPrintFormatter(printFormatter, startingAtPageAt: i) } } } override var numberOfPages: Int { textStorage.addLayoutManager(layoutManager) let size = CGFloat(50) for _ in 0..<2 { let textContainer = NSTextContainer(size: CGSize(width: size, height: size)) layoutManager.addTextContainer(textContainer) let textView = UITextView(frame: CGRect(x: 0, y: 0, width: size, height: size), textContainer: textContainer) textViews.append(textView) } return textViews.count } }My goal is printing a document from an attributed string and showing a custom header and footer on each page.
9
0
1.8k
Jan ’20
Detecting UITextView size change in order to set its textContainerInset
In my `UITextView` subclass i want to set the `textContainerInset` at the top and bottom to be half of the textview height. I thought that setting it in `UIViewController.viewWillLayoutSubviews()` or `UIViewController.viewDidLayoutSubviews` of the hosting view controller would be a good idea, but doing so breaks the textview: when I select a word in it and drag one of the selection handles down, the textview automatically scrolls up. What is the best way to detect a size change for a textview?
2
0
1.2k
Jan ’20
FileManager.trashItem(at:resultingItemURL:) never returns if called inside NSFileCoordinator.coordinate(writingItemAt:options:error:byAccessor:)
In the following code, the string "after" is never printed:class ViewController: UIViewController, UIDocumentPickerDelegate { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeText as String], in: .open) documentPicker.delegate = self present(documentPicker, animated: true) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { let url = urls[0] guard url.startAccessingSecurityScopedResource() else { return } defer { url.stopAccessingSecurityScopedResource() } DispatchQueue.global(qos: .background).async { var fcError, _error: NSError? NSFileCoordinator(filePresenter: nil).coordinate(writingItemAt: url, options: [.forMoving], error: &fcError, byAccessor: { url in print("before") do { try FileManager.default.trashItem(at: url, resultingItemURL: nil) // try FileManager.default.moveItem(at: url, to: url.deletingLastPathComponent().appendingPathComponent("bla")) } catch { _error = error as NSError } print("after") }) print(fcError ?? _error) } } }Other file operations (such as moving a file, see commented out line) work as expected. Commenting out the part with the NSFileCoordinator seems to solve the problem.
4
0
1.2k
Jan ’20
UITextView automatically scrolls upwards when dragging selection handles and textContainerInset is about half the textview height
Anyone else noticed this issue? Is there a workaround? The issue can be reproduced on an iPad Pro 11" with this code:class ViewController: UIViewController { @IBOutlet weak var textView: TextView! override func viewDidLoad() { super.viewDidLoad() let h = CGFloat(560) textView.textContainerInset = UIEdgeInsets(top: h, left: 0, bottom: 0, right: 0) } }
2
0
415
Mar ’20
Resolve bookmark created in iOS app in Share Extension
I create a URL bookmark with `URL.bookmarkData(options: [], includingResourceValuesForKeys: [.localizedNameKey])` and resolve it with `NSURL(resolvingBookmarkData: bookmarkData, options: [], relativeTo: nil, bookmarkDataIsStale: nil) as URL`. This works fine within my main app, but when sharing the bookmarkData via an App Group with my Share Extension, it gives the error "The file couldn't be opened because you don't have permission to view it.". Is there any way I can do this?
4
0
1k
Jun ’20
Getting inode number from URL
I couldn't find any other way of getting the inode number without using FileManager.attributesOfItem(atPath: url.path)[.systemFileNumber]. I'm already using FileManager.enumerator(at:includingPropertiesForKeys:errorHandler:) for enumerating large directories and using that other FileManager method only for accessing the inode number doubles the scan time. I looked for a URLResourceKey but there doesn't seem to be any. I would be really grateful for any kind of help.
26
0
4.4k
Aug ’20
Resolve bookmark created in iOS app in Share Extension
I create a URL bookmark with URL.bookmarkData(options: [], includingResourceValuesForKeys: [.localizedNameKey]) and resolve it with NSURL(resolvingBookmarkData: bookmarkData, options: [], relativeTo: nil, bookmarkDataIsStale: nil) as URL. This works fine within my main app, but when sharing the bookmarkData via an App Group with my Share Extension, it gives the error "The file couldn't be opened because you don't have permission to view it.". Is there any way I can do this?
0
0
249
Aug ’20
Swift string interpolation produces string that takes very long to insert in a dictionary
Under some conditions, string interpolation produces a string that is way slower to use as a dictionary key than a string produced with String(format:). Take the following code: var map = [String: String]() let start = Date() for _ in 0..<100000 {     var s = "asdf"     s = (s as NSString).appendingPathComponent("")     s = transform(s)     s = (s as NSString).substring(from: 1)     map[s] = s } print(-start.timeIntervalSinceNow) func transform(_ s: String) -> String {     return "\(s)/\(s)" //    return String(format: "%@/%@", s, s) } On my Mac I get the time interval 0.69 seconds printed out in the console (when using the string interpolation), but when commenting out line 13 and uncommenting line 14 (so that we use String(format:)) I get a 0.33 seconds time interval, less than half the time. Curiously, whenever uncommenting line 5 or line 7, string interpolation is faster. This took me quite a lot of time to figure out, since I would expect both methods to produce the same kind of string, but string interpolation to be always faster. Does anybody know why?
7
0
1.1k
Aug ’20