4

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

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

    for t in x {
        print(t)
    }
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
aotian16
  • 767
  • 1
  • 10
  • 21

2 Answers2

10

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.

Molanda
  • 661
  • 1
  • 6
  • 13
  • This is really a good idea. I try this and work fine. Could you also see my another question [how to detect if app use not system IME keyboard in swift](http://stackoverflow.com/questions/32706996/how-to-detect-if-app-use-not-system-ime-keyboard-in-swift) – aotian16 Sep 22 '15 at 05:26
  • Pretty smart. Hope apple will fix this ASAP. – Joe Nov 10 '15 at 22:06
5

It is a bug in Xcode 7 as mentioned HERE. Which says:

Summary:

Prior to the Xcode 7 GM, UITextInputMode.activeInputModes() returned an array of UITextInputMode instances. However, in the Xcode 7 GM, the method signature in the header file and documentation states that it returns an array of Strings, which is incorrect. As a result, code that uses activeInputModes correctly no longer compiles, and attempting to use activeInputModes in a Playground throws an exception.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • Hey here I am almost a year after this question was first asked and I just encountered this bug again. Any work around not envloving a Bridge-header for this issue? Or we need to wait one more year? thanks – Yakir Yehuda May 25 '16 at 13:56