标签:
String是Swift里新增加的类型,它与原来的NSString可以很方便地互相转换。但在实际开发中,我们该如何选择?
1
2
3
4
|
let words = "Hangge.com" for i in words{ print (i) //Hangge.com } |
2,要使用NSString的情况
1
2
3
4
|
let words = "Hangge.com" if (words as NSString ).containsString( "ge" ){ println ( "Yes" ) //YES } |
(2)String与Range配合比较麻烦,比如下面截取字符串的一部分,分别用转换成NSString再截取和直接使用String截取做演示,大家可以自行比较。(这个也只是麻烦些,其实问题也不大)
1
2
3
4
5
6
7
8
9
10
|
let words = "Hangge.com" //先转换成NSString var rangeStr1 = (words as NSString ).substringWithRange( NSMakeRange (4,2)) //ge //不转换 let index = advance (words.startIndex, 4) let index2 = advance (words.startIndex, 6) var range = Range < String . Index >(start: index, end: index2) var rangeStr2 = words.substringWithRange(range) //ge |
Swift - String与NSString的区别,以及各自的使用场景
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4843691.html