4

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

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

    for t in x {
        print(t)
    }
4

2 回答 2

10

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

桥接器.h

#ifndef Bridge_h
#define Bridge_h

#import "Kludge.h"

#endif

克鲁奇.h

#ifndef Kludge_h
#define Kludge_h

#import <UIKit/UITextInput.h>

@interface Kludge : NSObject

+ (NSArray<UITextInputMode *> *)activeInputModes;

@end

#endif

克鲁奇.m

#import "Kludge.h"

@implementation Kludge

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

@end

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

于 2015-09-22T03:51:55.930 回答
5

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

概括:

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

于 2015-09-21T09:49:54.590 回答