Reputation: 25964
I have tried the following:
let str = self.passwordLabel.text?.characters.dropLast()
let s = String(describing: str)
But it give 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))"
I just wanted to remove the last char from the string?
Upvotes: 3
Views: 1170
Reputation: 285069
You need to unwrap the optional string and don't use the describing
initializer.
if let passwordText = self.passwordLabel.text {
let s = String(passwordText.characters.dropLast())
}
or if it's guaranteed that text
is not nil
let s = String(self.passwordLabel.text!.characters.dropLast())
Upvotes: 3
Reputation: 19602
Do not use String(describing:...)
and unwrap the optional str
.
let passwordLabel = UILabel()
passwordLabel.text = "TEXT1"
if let str = passwordLabel.text?.characters.dropLast() {
let s = String(str)
print(s)
}
Prints: TEXT
Upvotes: 2