相关疑难解决方法(0)

Swift 1.2和Swift 2.0中的字符串长度

在以前版本的Swift中,我有以下代码.

func myfunc(mystr: String) {
    if mystr.utf16Count >= 3 {
Run Code Online (Sandbox Code Playgroud)

随着最新版本的Swift 1.2,我现在收到以下错误.

'utf16Count' is unavailable: Take the count of a UTF-16 view instead, i.e. count(str.utf16)
Run Code Online (Sandbox Code Playgroud)

好的,所以我按如下方式更改了我的代码.

func myfunc(mystr: String) {
    if count(mystr.utf16) >= 3 {
Run Code Online (Sandbox Code Playgroud)

但这不起作用.我现在得到以下错误消息.

'(String.UTF16View) -> _' is not identical to 'Int16'
Run Code Online (Sandbox Code Playgroud)

使用Swift 1.2获取字符串长度的正确方法是什么?

string swift

32
推荐指数
4
解决办法
5万
查看次数

标签 统计

string ×1

swift ×1