From cc0f4ab45c0d52f67d38c9ee44186f26034f9a8d Mon Sep 17 00:00:00 2001 From: Martin Eberl Date: Fri, 17 Aug 2018 08:36:56 +0200 Subject: [PATCH] backwards compatibility to swift3 --- .../xcschemes/PinCodeTextField.xcscheme | 3 +-- Pod/Extensions/StringExtension.swift | 17 +++++++++++++++++ Pod/PinCodeTextField.swift | 8 ++++---- Pod/TextHelper/TextHelper.swift | 4 ++-- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/PinCodeTextField.xcodeproj/xcshareddata/xcschemes/PinCodeTextField.xcscheme b/PinCodeTextField.xcodeproj/xcshareddata/xcschemes/PinCodeTextField.xcscheme index 4a06e6e..5e270a8 100644 --- a/PinCodeTextField.xcodeproj/xcshareddata/xcschemes/PinCodeTextField.xcscheme +++ b/PinCodeTextField.xcodeproj/xcshareddata/xcschemes/PinCodeTextField.xcscheme @@ -1,7 +1,7 @@ + version = "1.8"> @@ -56,7 +56,6 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - language = "" launchStyle = "0" useCustomWorkingDirectory = "NO" ignoresPersistentStateOnLaunch = "NO" diff --git a/Pod/Extensions/StringExtension.swift b/Pod/Extensions/StringExtension.swift index 5182a9d..4fb9a0b 100644 --- a/Pod/Extensions/StringExtension.swift +++ b/Pod/Extensions/StringExtension.swift @@ -12,4 +12,21 @@ internal extension String { var hasOnlyNewlineSymbols: Bool { return trimmingCharacters(in: CharacterSet.newlines).isEmpty } + + var length: Int { + #if swift(>=3.2) + return count + #else + return characters.count + #endif + } + + @available(swift 3.0) + @discardableResult mutating func removeLastCharacter() -> String { + #if swift(>=3.2) + return String(removeLast()) + #else + return String(characters.removeLast()) + #endif + } } diff --git a/Pod/PinCodeTextField.swift b/Pod/PinCodeTextField.swift index e23165a..f4c8c11 100644 --- a/Pod/PinCodeTextField.swift +++ b/Pod/PinCodeTextField.swift @@ -223,7 +223,7 @@ import UIKit } private func isPlaceholder(_ i: Int) -> Bool { - let inputTextCount = text?.count ?? 0 + let inputTextCount = text?.length ?? 0 return i >= inputTextCount } @@ -297,7 +297,7 @@ import UIKit let newText = text.map { $0 + character } ?? character let isNewline = character.hasOnlyNewlineSymbols let isCharacterMatchingCharacterSet = character.trimmingCharacters(in: allowedCharacterSet).isEmpty - let isLengthWithinLimit = newText.count <= characterLimit + let isLengthWithinLimit = newText.length <= characterLimit return !isNewline && isCharacterMatchingCharacterSet && isLengthWithinLimit } } @@ -324,7 +324,7 @@ extension PinCodeTextField: UIKeyInput { let newText = text.map { $0 + charToInsert } ?? charToInsert text = newText delegate?.textFieldValueChanged(self) - if (newText.count == characterLimit) { + if (newText.length == characterLimit) { if (delegate?.textFieldShouldEndEditing(self) ?? true) { let _ = resignFirstResponder() } @@ -334,7 +334,7 @@ extension PinCodeTextField: UIKeyInput { public func deleteBackward() { guard hasText else { return } - text?.removeLast() + text?.removeLastCharacter() delegate?.textFieldValueChanged(self) } } diff --git a/Pod/TextHelper/TextHelper.swift b/Pod/TextHelper/TextHelper.swift index f09b2a0..bfa1df2 100644 --- a/Pod/TextHelper/TextHelper.swift +++ b/Pod/TextHelper/TextHelper.swift @@ -20,8 +20,8 @@ class TextHelper { } func character(atIndex i: Int) -> Character? { - let inputTextCount = text?.count ?? 0 - let placeholderTextLength = placeholderText?.count ?? 0 + let inputTextCount = text?.length ?? 0 + let placeholderTextLength = placeholderText?.length ?? 0 let character: Character? if i < inputTextCount { let string = text ?? ""