在Beta3中工作正常,现在出现了一个奇怪的错误,而且我不知道怎么解决它。尝试了所有类似问题的解决方法。
|
1
2
3
4
5
6
7
8
9
10
11
|
if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(1) //Error } } if (acronym as NSString).length > 2 { acronym = acronym.substringToIndex(2) //Error } } |
标记线上给了我相同的错误:手
|
1
|
Type 'String.Index' does not conform protocol 'IntegerLiteralConvertible' |
在beta4中,Swift的String.Index处理再一次修改—你现在不能提供一个Int当String.Index被预期。处理它的方法是通过创建String.Index
你需要使用预先方法:
|
1
2
3
4
5
6
7
8
9
10
11
|
if !name.isEmpty { var splitted: [String] = name.componentsSeparatedByString(" ") for curPart in splitted { if !curPart.isEmpty { acronym += curPart.substringToIndex(advance(curPart.startIndex, 1)) } } if (acronym as NSString).length > 2 { acronym = acronym.substringToIndex(advance(acronym.startIndex, 2)) }} |
本文解决了在Swift Beta4中遇到的字符串索引错误问题,提供了正确的代码示例来处理字符串索引,确保代码能够正确运行。

146

被折叠的 条评论
为什么被折叠?



