ios – 在Swift中采用UITextInputTraits

我试图在
Swift 3中采用UITextInputTraits作为自定义UIButton子类.

我添加了“Symbols” section of the documentation中指定的所有属性:

import UIKit 

class TextInputButton: UIButton {
    var autocapitalizationType: UITextAutocapitalizationType = .none
    var autocorrectionType: UITextAutocorrectionType         = .no
    var spellCheckingType: UITextSpellCheckingType           = .no
    var enablesReturnKeyAutomatically: Bool                  = false
    var keyboardAppearance: UIKeyboardAppearance             = .default
    var keyboardType: UIKeyboardType                         = .default
    var returnKeyType: UIReturnKeyType                       = .default
    var isSecureTextEntry: Bool                              = false

    // (...rest of class implementation omitted...)

但是,当我尝试编译时,isSecureTextEntry会导致以下错误:

Objective-C method ‘setIsSecureTextEntry:’ provided by setter for
‘isSecureTextEntry’ does not match the requirement’s selector
(‘setSecureTextEntry:’)

如果我使用建议的’fix-it’,则属性声明变为:

var @objc(setSecureTextEntry:) isSecureTextEntry: Bool = false

…但现在我得到了三个错误:

>预期模式
>一行上的连续声明必须用’;’分隔

(要我在var和@objc之间插入一个分号),

和:

>预期宣言

实现这一属性的正确方法是什么?

最佳答案 也许不完全是你的情况,但是这种UITextInputTraits的采用对我来说很好

(次要提示:UIKeyInput扩展UITextInputTraits)

public class ShortPasswordEntry : UIControl, UIKeyInput
{
    public override init(frame: CGRect) {
        super.init(frame: frame)
    }

    extension ShortPasswordEntry
    {
        public var keyboardType: UIKeyboardType {
            get {
                return .numberPad
            }
            set {
                assertionFailure()
            }
        }
    }
}
点赞