UITextInputMode.activeInputModes() crashes in Swift 2

耗尽温柔 提交于 2020-01-30 05:25:21

问题


I want to get UITextInputMode in Swift 2 but UITextInputMode.activeInputModes() crashes.

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

    for t in x {
        print(t)
    }

回答1:


It is a bug in Xcode 7 as mentioned HERE. Which says:

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.




回答2:


I was able to work around this bug by using an Objective-C bridge.

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

From Swift, you can now call Kludge.activeInputModes() and get the correct results.



来源:https://stackoverflow.com/questions/32691424/uitextinputmode-activeinputmodes-crashes-in-swift-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!