问题描述
使用Beta 3一切正常,现在我得到一个奇怪的错误,我不知道如何解决它。尝试了类似问题的所有解决方案。
With Beta 3 all worked fine, now I get a strange error, and I have no clue how to fix it. Tried all the solutions for similiar problems.
这是我的代码:
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
}
}
两条标记的行都给出了同样的错误:
Both marked lines gave me the same error:
有人能帮帮我吗?或者Beta 4被窃听?
谢谢!
Can someone help me? Or is Beta 4 bugged?Thanks!
推荐答案
在测试版4中,Swift的String.Index处理再次改变了 - 你现在不能当需要 String.Index
时,提供 Int
。处理它的方法是创建 String.Index
,你需要使用 advance
方法:
In beta 4, Swift's String.Index handling changed yet again -- you now can't supply an Int
when a String.Index
is expected. The way to handle it is by creating the String.Index
you need using the advance
method:
if !name.isEmpty {
var splitted: [String] = name.componentsSeparatedByString(" ")
for curPart in splitted {
if !curPart.isEmpty {
acronym += curPart.substringToIndex(advance(curPart.startIndex, 1))
}
}
if countElements(acronym) > 2 {
acronym = acronym.substringToIndex(advance(acronym.startIndex, 2))
}
}
这都是基于确保正确处理Unicode字符串 - 因为不同的Unicode字符可以有不同的大小,纯整数索引会隐藏字符串不是随机访问的事实。
This is all based on making sure Unicode strings are handled properly - since different Unicode characters can have different sizes, pure integer indexing would hide the fact that Strings aren't random access.
这篇关于类型'String.Index'不符合协议'IntegerLiteralConvertible'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!