UITextInputMode.activeInputModes()在Swift 2中崩溃

aot*_*n16 4 ios uitextinput swift swift2

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

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

    for t in x {
        print(t)
    }
Run Code Online (Sandbox Code Playgroud)

Mol*_*nda 10

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

Bridge.h

#ifndef Bridge_h
#define Bridge_h

#import "Kludge.h"

#endif
Run Code Online (Sandbox Code Playgroud)

Kludge.h

#ifndef Kludge_h
#define Kludge_h

#import <UIKit/UITextInput.h>

@interface Kludge : NSObject

+ (NSArray<UITextInputMode *> *)activeInputModes;

@end

#endif
Run Code Online (Sandbox Code Playgroud)

Kludge.m

#import "Kludge.h"

@implementation Kludge

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

@end
Run Code Online (Sandbox Code Playgroud)

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


Dha*_*esh 5

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

摘要:

在Xcode 7 GM之前,UITextInputMode.activeInputModes()返回了一组UITextInputMode实例.但是,在Xcode 7 GM中,头文件和文档中的方法签名声明它返回一个字符串数组,这是不正确的.因此,activeInputModes正确使用的代码不再编译,并且尝试activeInputModes在Playground中使用会抛出异常.