我試圖顯示我的聯繫人姓名的首字母大寫爲索引:字符串不能以「誠信」 Swift3
var firstLetter: String? = !(contact?.firstName == "") ? (contact?.firstName as? String)?.substring(to: 1).uppercased() : (contact?.lastName as? String)?.substring(to: 1).uppercased()
,但我有此錯誤:
String may not be indexed with 'Int', it has variable size elements
'String'由'String.Index'索引,而不是'詮釋',比較[String.Index如何在Swift 3中工作](https://stackoverflow.com/q/39676939/2976878)。雖然在你的情況下,你可以使用'string.characters.first',它會給你的字符串的第一個字符,或'nil'如果字符串是空的 - 比較[如何獲得數組中的所有字符串的第一個字母在iOS Swift中?](https://stackoverflow.com/q/33792991/2976878)。 – Hamish
這裏聯繫什麼類型?你能顯示「聯繫」類型的定義嗎? – PGDev