Characters Is Unavailable' Please Use String Directly

How to fix 'characters' is unavailable: Please use String directly in swift 5

You can remove characters to use String directly.

For example

var anotherNumbers = "0123456789"
var numbers = String(anotherNumbers.filter { "01234567890.".contains($0) })

returns "0123456789"

warning: 'characters' is deprecated: Please use String or Substring directly

Swift 4 introduced changes on string API.

You can just use !stringValue.isEmpty instead of stringValue.characters.count > 0

for more information you get the sample from here

for e.g

let edit = "Summary"
edit.count // 7

characters is unavailable' please use string directly

Your code should work as it is if you delete the characters I suggest you create a new playground file. Btw you can simply use RangeReplaceableCollection mutating method popLast and iterate while your string is not empty to avoid calling your collection count property multiple times:

var shrinking = "hello"
repeat {
print(shrinking)
shrinking.popLast()
} while !shrinking.isEmpty

This will print

hello

hell

hel

he

h

or using removeLast method but it requires the string not to be empty so you would need to check if the string is empty at before the closure:


var shrinking = "hello"

while !shrinking.isEmpty {
print(shrinking)
shrinking.removeLast()
}

Swift 4 : substring(with:)' is deprecated: Please use String slicing subscript

The someString.substring(with: someRange) just needs to be someString[someRange].

So change:

entity.substring(with: entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1))

with

entity[entity.index(entity.startIndex, offsetBy: 3) ..< entity.index(entity.endIndex, offsetBy: -1)]

In other words, change the .substring(with: to [ and change the closing ) to ].

The result is a Substring, not String. So you may need to wrap the result in String( ) to get a String from the substring result.

How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

You should leave one side empty, hence the name "partial range".

let newStr = str[..<index]

The same stands for partial range from operators, just leave the other side empty:

let newStr = str[index...]

Keep in mind that these range operators return a Substring. If you want to convert it to a string, use String's initialization function:

let newStr = String(str[..<index])

You can read more about the new substrings here.

How to filter characters from a string in Swift 4

Swift 4 makes it a little simpler. Just remove the .characters and use

let myNumbers = myString.filter { "0123456789".contains($0) }

But to really do it properly, you might use the decimalDigits character set...

let digitSet = CharacterSet.decimalDigits
let myNumbers = String(myString.unicodeScalars.filter { digitSet.contains($0) })

How can I rewrite this in swift 4 or 5

Simply remove .characters

extension LoginViewController : UITextFieldDelegate {

func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {

if let username = self.usernameField.text,
let password = self.passwordField.text {

if username.count > 0 &&
password.count > 0 {
self.loginButton.isEnabled = true
}
}

return true
}

}


Related Topics



Leave a reply



Submit