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
What's the Rationale of Swift's Size Methods Taking 'Int'S
Swift. Declaring Private Functions in Internal Protocol
Handling an Attribute of an Xml Element in Swift
How to Masking the Last Number in Swift
Crash When Running on Device After Second Launch
Text from [String] to a Label.Text Isn't Working the First Time
Swift: Label Text --> "Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value"
How to Draw Dashed Line in Arkit (Scenekit) Like in the Measure App
Swiftui Casting Tupleview to an Array of Anyview
Swift: Accessing Computed Property Through Pointer
Explicitly Unwrapping Optional Nil Does Not Cause Crash
Navigationview Swiftui Shows Split View in iPad
Resizing Uimage When Using Sf Symbols - Uiimage(Systemname:)
Does the Initializer of an 'Open' Class Need to Be Open as Well
How to Reference the View's Window in Swift 3.X Using Storyboards/Cocoa
Swift: Google Maps Draw Waypoint Polyline
Display Table View When Searchbar (From Searchcontroller) Begin Edited Swift