【发布时间】:2015-06-16 23:33:42
【问题描述】:
在以前版本的 Swift 中,我有以下代码。
func myfunc(mystr: String) {
if mystr.utf16Count >= 3 {
使用最新版本的 Swift 1.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 获取字符串长度的正确方法是什么?
【问题讨论】:
-
右键点击计数,然后“跳转到定义”。
-
从 Swift 4+ 你可以使用,
str.count