在Objective-C中我们通常使用length来获取NSString类型的字符串的长度,而在Swift中,String结构体没有了这个方法,那怎样获取String类型的字符串的长度呢?我们可以通过他的扩展属性成员characters
的count
属性来获取
let titleString : String = "title"
let count = titleString.characters.count //获取String字符串的长度
print(count)
注意:在xcode9.0中,swift4.0 直接去掉characters就可以了例如 titleString.count
关注IT美学公众号,分享更多知识