Giter VIP home page Giter VIP logo

Comments (4)

AlexisQapa avatar AlexisQapa commented on August 18, 2024

Just use pre defined characters set with unions like
Notation(character: "N", characterSet: CharacterSet(charactersIn: "-").union(CharacterSet.letters), isOptional: false), Then use [N...]

from input-mask-ios.

taflanidi avatar taflanidi commented on August 18, 2024

Mr. @AlexisQapa, thanks for your help.

@SergeyGamayunov, please let us know if this solution fits your needs.

let listener: MaskedTextFieldDelegate
listener.customNotations = [
  Notation(
    character: "N",
    characterSet: CharacterSet(charactersIn: "-").union(CharacterSet.letters),
    isOptional: false
  ),
]
listener.primaryMaskFormat = "[N…]"

from input-mask-ios.

SergeyGamayunov avatar SergeyGamayunov commented on August 18, 2024

Hey, I just tested it. In a few words - yes, it does, but I had to create some nice workaround for convenient work. In case if you or anybody else would be interested:

That enum represents different character sets. We will combine them in the future.

enum Characters {
        case digits, letters, spaceDash, dotNumber
        
        var set: CharacterSet {
            switch self {
            case .digits:
                return CharacterSet.decimalDigits
            case .letters:
                return CharacterSet.letters
            case .spaceDash:
                return CharacterSet(charactersIn: " -")
            case .dotNumber:
                return CharacterSet(charactersIn: ".№")
            }
        }
    }

This enum represents different validation types, and variable for constructing custom notations for every validation type (which are also represented by enum):

enum ValidationType {
    case name, oms

    private var notations: [Notation] {
        var set: [Characters]
        var letter: Character
        switch self {
        case .name:
            set = [.letters, .spaceDash]
            letter = "N"
        case .oms:
            set = [.letters, .digits, .dotNumber, .spaceDash]
            letter = "O"
        default:
            return []
        }
        
        guard let allowedCharacters = characterSet(unionWith: set) else { return [] }
        return [Notation(character: letter, characterSet: allowedCharacters, isOptional: false)]
    }

Please, be aware, that this is a sample code, it also includes format, mask and listener variables which are not specific for current topic

from input-mask-ios.

taflanidi avatar taflanidi commented on August 18, 2024

Well, @SergeyGamayunov, your code looks overengineered to me.

First of all, why won't you just extend CharacterSet?
Second, your enum is just an abstraction for the sake of abstraction. Especially at this point:

case .digits:
    return CharacterSet.decimalDigits
case .letters:
    return CharacterSet.letters

Third, here,

guard let allowedCharacters = characterSet(unionWith: set) else { return [] }

— you are creating a disconnected coupling problem, which is a bad design smell.

Bad design like this eventually leads to bug misunderstanding.
In your particular case, MaskedTextFieldDelegate would throw a mask compilation error exception, while MaskedTextFieldDelegate nor its internals doesn't have anything with the actual bug: CharacterSet initialisation failure.

Fourth, a minor one. Please, replace

var set: [Characters]
var letter: Character

with

let set: [Characters]
let letter: Character

from input-mask-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.