当我尝试使用最新版本的Swift 设置label1为第一个字母时,我在最后一行收到错误string.如何解决这个问题?
let preferences = UserDefaults.standard
let name1 = "" + preferences.string(forKey: "name")!
let name2 = name1
label1.text = name2.substring(from: 0)
Run Code Online (Sandbox Code Playgroud)
ale*_*nik 11
那是因为substring方法接受String.Index而不是普通Int.试试这个:
let index = name2.index(str.startIndex, offsetBy: 0) //replace 0 with index to start from
label.text = name2.substring(from: index)
Run Code Online (Sandbox Code Playgroud)
它要求一个索引,而不是一个整数。
let str = "string"
print(str.substring(from: str.startIndex))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2883 次 |
| 最近记录: |