Using last
to get the last Character
For Swift 4:
let str = "hello world??"
let lastChar = str.last! // lastChar is "??"
For Swift 2 and Swift 3:
let str = "hello world??"
let lastChar = str.characters.last! // lastChar is "??"
For Swift 1.2:
let str = "hello world??"
let lastChar = last(str)! // lastChar is "??"
Subscripting the String
to get the last Character
This works for Swift 3 and Swift 4:
let str = "hello world??"
let lastChar = str[str.index(before: str.endIndex)] // lastChar is "??"
And for Swift 1.2 and Swift 2:
let str = "hello world??"
let lastChar = str[str.endIndex.predecessor()] // lastChar is "??"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…