UITextInputMode.activeInputModes()在Swift 2中崩溃

我想在Swift 2中得到UITextInputMode,但UITextInputMode.activeInputModes()崩溃。

  let x = UITextInputMode.activeInputModes() // crash here for t in x { print(t) } 

这是在这里提到的Xcode 7中的一个错误。 其中说:

概要:

在Xcode 7 GM之前, UITextInputMode.activeInputModes()返回一个UITextInputMode实例的数组。 但是,在Xcode 7 GM中,头文件和文档中的方法签名指出它返回的string数组不正确。 因此,正确使用activeInputModes代码不再编译,并尝试在Playground中使用activeInputModes将引发exception。

我能够通过使用Objective-C桥来解决这个bug。

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 

从Swift开始,您现在可以调用Kludge.activeInputModes()并获得正确的结果。