UITextInputMode.activeInputModes() crashes in Swift 2

后端 未结 2 714
生来不讨喜
生来不讨喜 2021-01-19 09:28

I want to get UITextInputMode in Swift 2 but UITextInputMode.activeInputModes() crashes.

    let x = UITextInputMode.activeInputModes() // crash         


        
2条回答
  •  無奈伤痛
    2021-01-19 09:53

    I was able to work around this bug by using an Objective-C bridge.

    Bridge.h

    #ifndef Bridge_h
    #define Bridge_h
    
    #import "Kludge.h"
    
    #endif
    

    Kludge.h

    #ifndef Kludge_h
    #define Kludge_h
    
    #import 
    
    @interface Kludge : NSObject
    
    + (NSArray *)activeInputModes;
    
    @end
    
    #endif
    

    Kludge.m

    #import "Kludge.h"
    
    @implementation Kludge
    
    + (NSArray *)activeInputModes {
      return (NSArray *)[UITextInputMode activeInputModes];
    }
    
    @end
    

    From Swift, you can now call Kludge.activeInputModes() and get the correct results.

提交回复
热议问题