I get error in the last line when I try to set label1
to the first letter of string
, using latest version of Swift. How to solve this issue?
let preferences = UserDefaults.standard
let name1 = "" + preferences.string(forKey: "name")!
let name2 = name1
label1.text = name2.substring(from: 0)
That's because substring
method accepts String.Index
instead of plain Int
. Try this instead:
let index = name2.index(str.startIndex, offsetBy: 0) //replace 0 with index to start from
label.text = name2.substring(from: index)
the first letter of the string in Swift 3 is
label1.text = String(name2[name2.startIndex])
String could not be indexed by Int
already in Swift 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With