Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Swift 4 vs Swift 3 examples:

let myString = "test"

for char in myString.characters {print(char) } // Swift 3
for char in myString { print(char) } // Swift 4

let length = myString.characters.count // Swift 3
let length = myString.count // Swift 4

One of the most common cases for manipulating strings is with JSON responses. In this example I created an extension in my watch app to drop the last (n) characters of a Bitcoin JSON object.

Swift 3:

func dropLast(_ n: Int = 0) -> String {
    return String(characters.dropLast(n))

Xcode 9.1 Error Message:

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

Xcode is telling us to use the string variable or method directly.

Swift 4:

func dropLast(_ n: Int = 0) -> String {
    return String(dropLast(n))
    }

Complete Extension:

extension String {
    func dropLast(_ n: Int = 0) -> String {
        return String(dropLast(n))
    }

    var dropLast: String {
        return dropLast()
    }
}

Call:

print("rate:\(response.USDRate)")
let literalMarketPrice = response.USDRate.dropLast(2)
print("literal market price: \(literalMarketPrice)")

Console:

//rate:7,101.0888 //JSON float
//literal market price: 7,101.08 // JSON string literal

Additional Examples:

  • print("Spell has \(invisibleSpellName.count) characters.")
  • return String(dropLast(n))
  • return String(removeLast(n))

Documentation:

You'll often be using common methods such as dropLast() or removeLast() or count so here is the explicit Apple documentation for each method.

droplast()

removelast()

counting characters


Use this characters because String stopped being a collection in Swift 2.0. However this is still valid code in Swift 4 but is no longer necessary now that String is a Collection again.

For example a Swift 4 String now has a direct count property that gives the character count:

// Swift 4
let spString = "Stack"
spString.count           // 5

Examples for String and SubString.

String

Swift 4 String now directly get Element that gives the first character of String: (string.characters.first)

let spString = "Stack"
let firstElement = spString.first   //S

SubString

Using SubString get first character.

let spstring = "Welcome"
let indexStartOfText = spstring.index(spstring.startIndex, offsetBy: 1)
let sub = spstring.substring(to: indexStartOfText)
print(sub) //W

That warning is just a top of the iceberg, there were a loot of string changes, strings are again a collection of characters, but we got soemthing new and cool, subStrings :)

This is a great read about this: https://useyourloaf.com/blog/updating-strings-for-swift-4/


Just remove characters For example:

stringValue.characters.count to stringValue.count