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

Krunal picture Krunal · Sep 28, 2017 · Viewed 71.6k times · Source

characters - an instance property of String, is deprecated from with Xcode 9.1

It was very useful to get a substring from String by using the characters property but now it has been deprecated and Xcode suggests to use substring. I've tried to check around SO questions and apple developer tutorials/guidelines for the same. But could not see any solution/alternate as suggested.

Here is warning message:

'characters' is deprecated: Please use String or Substring

enter image description here

I've so many string operations are performed/handled using property characters.

Anyone have any idea/info about this update?

Answer

songxunzhao picture songxunzhao · Sep 28, 2017

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