UITextInputMode.activeInputModes() 在 Swift 2 中崩溃

UITextInputMode.activeInputModes() crashes in Swift 2

我想在 Swift 2 中获取 UITextInputMode,但 UITextInputMode.activeInputModes() 崩溃了。

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

    for t in x {
        print(t)
    }

HERE所述,这是 Xcode 7 中的错误。其中说:

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.

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

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() 并获得正确的结果。