There is a growing number of swift projects that may often see str as nsstring or str as The string string is switched back and forth between the two, because some operations are easier with OC strings, while some operate on the contrary, mastering the characteristics of both of these strings is necessarily beneficial to the development of the project. This article is some of their own understanding, drawing on the official documents, such as the understanding of deviations also welcome guidance.
If you do not see this article in the Dong Yuan Blog Park, please click to view the original text.
First, the common denominator is that string retains most of the NSString APIs such as
. hasprefix
. lowercasestring
. componentsseparatedbystring
. Substringwithrange et cetera
So a lot of normal operations in the development of using either of the two are possible, the following is the difference.
NSString is a reference type. Swiftstring is a value type.
var nsstring:nsstring = NSString () var swiftstring:string = String () var nsstring:nsstring = "Dsx" var swiftstring:string = "DSX"
Both can use their own class name to initialize directly, the following method is also initialized, although the same way, but nsstring means that the initialization of a pointer to the string, but the meaning of swiftstring is to assign the string literal to the variable.
swiftstring concatenation between strings is easier than nsstring
NSString need to stitch two strings with append or stringWithFormat
NSString *stra = @ "My name"; NSString *STRB = @ "is dsx"; Stra = [Stra STRINGBYAPPENDINGSTRING:STRB];
and swiftstring only need to use "+" can
var stra = "My Name" var StrB = "is Dsx" stra = "My Name" + "is dsx" stra = stra + StrB println (stra)
The first syntax in Swift is to allow the use of "+" stitching between strings and characters, but swift1.2 cannot do so, and strings and characters can only be append.
Swiftstring can implement string traversal
Swiftstring can implement traversal of all characters inside a string, which nsstring cannot do because the former inherits the CollectionType protocol (swift2 no longer inherits this protocol, Change to provide a collection of characters by a characters property)
swift1.2 for charater in ' My name is Dsx ' { println (charater) } //swift2.0 for character in "My NA Me is Dsx ". Characters { print (character) }
Swift's string is more like a combination of multiple character elements and is a product of a sequence, like but not a collection class, and if you add an as nsstring to the above "This is a book", the following error will be reported:
Type ' nsstring ' does not conform to protocol ' Sequencetype '
Write as String after swift2.0 will also report the above error, 2.0 the original string inherited the Sequencetype and CollectionType protocol sank to a string. Characterview to inherit the idea that Apple is doing this to reduce the waste of resources.
Swiftstring the method of calculating string length differs from NSString
var stra:string = "MC Dream" var strb:nsstring = "MC Dream" Print (stra.lengthofbytesusingencoding (nsutf8stringencoding )//5 print (StrA.characters.count))//3 print (strb.length);//3
NSString uses the string directly. length to get the string lengths, but string knocks. Length can only be knocked out . Lengthofbytesusingencoding(nsutf8stringencoding) is 3 times times as much storage space for Chinese characters as English. The way Swift really resembles. Lengh is to take out the characters Property (array) and then. Count
Swiftstring Comparison of string equality in the same way as NSString
Let stra:nsstring = ' let strb:nsstring = ' let strc:nsstring = ' dsx ' let strd:nsstring = ' dsx ' if (s Tra.isequaltostring (StrB as String) { print ("yes"); } if (strc = = StrD) { print ("yes"); }
NSString has a method isequaltostring method used to determine whether two strings are exactly equal, string does not have this method, but because string is a value type, you can use = = to determine whether it is exactly equal.
NSString can see conversions with basic data types
var stra:nsstring = "12306" var strb:nsstring = "0.618" var numofint = stra.integervalue; var numofdouble = Strb.doublevalue;
This feature is not swift, so it may be convenient to use nsstring in JSON parsing or dictionary-to-model.
Swiftstring can be used to determine whether the string is empty by the IsEmpty property
var emptyStr = "" if Emptystr.isempty { print ("It is Empty") }
This is also a unique attribute of swiftstring. (Dong Jiahan) general NSString want to determine whether the string is empty is required to see. length is >0. Judging NSString = = Nil This method is now no longer used, will error
Binary operator ' = = ' cannot is applied to operands of type ' nsstring ' and ' nilliteralconvertible '
swiftstring unique string insertion character function
var stra:string = "My name is DX" stra.insert ("s", AtIndex:strA.characters.indexOf ("x")!); Print (Stra)//My name is DSX
This method can only insert a single character cannot be inserted string, if written in "SS" will error cannot convert value of type ' string ' to expected argument type ' Character '
If you do not see this article in the Dong Yuan Blog Park, please click to view the original text.
NSString is a pointer that cannot do this. And string also has the function of NSString Substringfromindex or Toindex.
Summarize:
The overall function of string is more powerful than nsstring, after all, the regular army in Swift grammar. There are a lot of differences, even if he has a function that you don't have. In the end, it is a value type and a reference type. Some of the attributes are extended after swift2.0, which makes the invocation of many methods of string a huge change. There are some differences are being explored, I hope this article can be helpful to the reader, reproduced must be reproduced and the original link.
NSString and swiftstring differences and usage scenarios