Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextInputMode.activeInputModes() crashes in Swift 2

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

    let x = UITextInputMode.activeInputModes() // crash here

    for t in x {
        print(t)
    }
like image 902
aotian16 Avatar asked Dec 06 '22 20:12

aotian16


1 Answers

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 <UIKit/UITextInput.h>

@interface Kludge : NSObject

+ (NSArray<UITextInputMode *> *)activeInputModes;

@end

#endif

Kludge.m

#import "Kludge.h"

@implementation Kludge

+ (NSArray<UITextInputMode *> *)activeInputModes {
  return (NSArray<UITextInputMode *> *)[UITextInputMode activeInputModes];
}

@end

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

like image 105
Molanda Avatar answered Dec 10 '22 12:12

Molanda