在Swift的前一个版本中,我有以下代码。
func myfunc(mystr: String) {
    if mystr.utf16Count >= 3 {使用SWIFT1.2的最新版本,我现在得到了以下错误。
'utf16Count' is unavailable: Take the count of a UTF-16 view instead, i.e. count(str.utf16)因此,我修改代码如下:
func myfunc(mystr: String) {
    if count(mystr.utf16) >= 3 {但这不管用。现在我得到以下错误消息。
'(String.UTF16View) -> _' is not identical to 'Int16'使用Swift 1.2获得字符串长度的正确方法是什么?
发布于 2015-04-11 07:13:23
count(mystr)是正确的方式,您不需要转换编码。
这是:只要你做了if count(mystr.utf16) >= 3,Int16(3)就可以了
编辑:这是一个旧的答案。OP更新了他的问题以反映Swift 2,上面的答案是正确的。
https://stackoverflow.com/questions/29575140
复制相似问题