1

I had been working making a swift implementation of this question iPhone: Change Keyboard language programmatically but I found something that i can't figure out how to fix,

This method crash my application

UITextInputMode.activeInputModes() //this crashes

This method is the analog in swift of this method in Objective C

 [UITextInputMode activeInputModes] //this works ok

This is the crash

Screen Shot

So my question is: Is this a bug in Swift? Or am I doing something wrong?

jvarela
  • 3,744
  • 1
  • 22
  • 43
Reinier Melian
  • 20,519
  • 3
  • 38
  • 55

2 Answers2

3

This seems to be a bug of iOS 9 SDK.

Objective-C header as follows:

+ (NSArray<NSString *> *)activeInputModes; // The activate input modes.

This should be:

+ (NSArray<UITextInputMode *> *)activeInputModes; // The activate input modes.

A dirty workaround:

let aaa = (UITextInputMode.self as AnyObject as! NSObject).performSelector("activeInputModes").takeRetainedValue()

You should send a Bug Report to Apple soon.

OOPer
  • 47,149
  • 6
  • 107
  • 142
1

UITextInputMode.activeInputModes() returns the wrong type and crashes in Xcode 7, see here .

And how to avoid it , I saw the solution to it likes this(create a bridge between OC and swift):

MyMethod.h

#import <UIKit/UITextInput.h>

@interface MyMethod: NSObject

+ (NSArray<UITextInputMode *> *)activeInputModes;

@end

MyMethod.m

#import "MyMethod.h"
 @implementation MyMethod

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

Bridge.h

#import "MyMethod.h"

You can now call MyMethod.activeInputModes() and get the correct results in swift.

CJiYI
  • 55
  • 1
  • 4