String.CharacterView - how to convert to String

I've tried the following:

let str = self.passwordLabel.text?.characters.dropLast()
let s = String(describing: str)

      

But this gives me the following:

"Optional(Swift.String.CharacterView(_core: Swift._StringCore(_baseAddress: Optional(0x0000618000051e10), _countAndFlags: 9223372036854775810, _owner: Optional(Swift._HeapBufferStorage<Swift._StringBufferIVars, Swift.UInt16>)), _coreOffset: 0))"

      

Do I just need to remove the last char from a string?

+3


source to share


2 answers


You need to expand the optional string and not use an initializer describing

.

if let passwordText = self.passwordLabel.text {
    let s = String(passwordText.characters.dropLast())
}

      



or if he guarantees that he text

does notnil

let s = String(self.passwordLabel.text!.characters.dropLast())

      

+3


source


Don't use String(describing:...)

and expand optional str

.

let passwordLabel = UILabel()
passwordLabel.text = "TEXT1"
if let str = passwordLabel.text?.characters.dropLast() {
    let s = String(str)
    print(s)
}

      



Prints: TEXT

+2


source







All Articles