我正在使用InputMethodKit编写一个快速输入法 (IM) 。IM 基本上可以工作,我从 Konsole 中获得击键inputText()
并查看我的日志消息。
现在我composedString(_ sender: Any!)
像这样快速实现:
override func composedString(_ sender: Any!) -> Any! {
let ret = NSMutableString().append("test")
return ret
}
我知道composedString(_ sender: Any!)
我打电话时系统会调用它updateComposition()
。情况确实如此,但composedString(_ sender: Any!)
确实会崩溃显示该输出:
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libswiftCore.dylib 0x00007fff2ccbd680 swift_unknownObjectRetain + 32
1 com.IBNSoft.inputmethod.MyApp 0x000000010c08c491 @objc InputController.composedString(_:) + 33
2 com.apple.InputMethodKit 0x00007fff4670358d -[IMKInputController updateComposition] + 45
3 com.IBNSoft.inputmethod.MyApp 0x000000010c08a5bf closure #1 in InputController.inputText(_:client:) + 703 (InputController.swift:25)
4 com.IBNSoft.inputmethod.MyApp 0x000000010c086740 thunk for @escaping @callee_guaranteed () -> () + 48
5 libdispatch.dylib 0x00007fff20441603 _dispatch_call_block_and_release + 12
6 libdispatch.dylib 0x00007fff204427e6 _dispatch_client_callout + 8
7 libdispatch.dylib 0x00007fff2044eb2f _dispatch_main_queue_callback_4CF + 940
8 com.apple.CoreFoundation 0x00007fff207226f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9 com.apple.CoreFoundation 0x00007fff206e48e2 __CFRunLoopRun + 2755
10 com.apple.CoreFoundation 0x00007fff206e375c CFRunLoopRunSpecific + 563
11 com.apple.HIToolbox 0x00007fff28905203 RunCurrentEventLoopInMode + 292
12 com.apple.HIToolbox 0x00007fff28904f65 ReceiveNextEventCommon + 587
13 com.apple.HIToolbox 0x00007fff28904d03 _BlockUntilNextEventMatchingListInModeWithFilter + 70
14 com.apple.AppKit 0x00007fff22edfb32 _DPSNextEvent + 864
15 com.apple.AppKit 0x00007fff22ede305 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1364
16 com.apple.AppKit 0x00007fff22ed0679 -[NSApplication run] + 586
17 com.apple.AppKit 0x00007fff22ea485c NSApplicationMain + 816
18 com.IBNSoft.inputmethod.MyApp 0x000000010c08935d main + 13 (AppDelegate.swift:19)
19 libdyld.dylib 0x00007fff20607f3d start + 1
苹果文档说composedString(_:)
:
返回值:
当前组合的字符串,可以是 NSString 或 NSAttributedString 对象。返回的对象应该是一个自动释放的对象。
我想问题是我没有NSMutableString
正确分配“自动释放和保留”。任何人都可以阐明崩溃的问题是什么以及如何解决它。
谢谢