Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adopting UITextInputTraits in Swift

I am trying to adopt the UITextInputTraits for a custom UIButton subclass in Swift 3.

I added all the properties specified in the "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...)

However, when I try to compile, isSecureTextEntry causes the following error:

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

If I use the suggested 'fix-it', the property declaration turns into:

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

...but now I get the three errors:

  1. Expected Pattern

  2. Consecutive declarations on a line must be separated by ';'

(wants me to insert a semicolon between var and @objc),

and:

  1. Expected Declaration

What is the correct way of implementing this one property?

like image 607
Nicolas Miari Avatar asked Oct 30 '22 12:10

Nicolas Miari


1 Answers

Maybe not exactly your case, but such adoption of UITextInputTraits worked fine for me

(minor hint: UIKeyInput extends 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()
            }
        }
    }
}
like image 141
Tieru Avatar answered Nov 15 '22 07:11

Tieru