Swift - which types to use? NSString or String Swift - which types to use? NSString or String ios ios

Swift - which types to use? NSString or String


You should use the Swift native types whenever possible. The language is optimized to use them, and most of the functionality is bridged between the native types and the Foundation types.

While String and NSString are mostly interchangeable, i.e, you can pass String variables into methods that take NSString parameters and vice versa, some methods seem to not be automatically bridged as of this moment. See this answer for a discussion on how to get the a String's length and this answer for a discussion on using containsString() to check for substrings. (Disclaimer: I'm the author for both of these answers)

I haven't fully explored other data types, but I assume some version of what was stated above will also hold true for Array/NSArray, Dictionary/NSDictionary, and the various number types in Swift and NSNumber

Whenever you need to use one of the Foundation types, you can either use them to type variables/constants explicitly, as in var str: NSString = "An NSString" or use bridgeToObjectiveC() on an existing variable/constant of a Swift type, as in str.bridgeToObjectiveC().length for example. You can also cast a String to an NSString by using str as NSString.

However, the necessity for these techniques to explicitly use the Foundation types, or at least some of them, may be obsolete in the future, since from what is stated in the language reference, the String/NSString bridge, for example, should be completely seamless.

For a thorough discussion on the subject, refer to Using Swift with Cocoa and Objective-C: Working with Cocoa Data Types


NSString : Creates objects that resides in heap and always passed by reference.

String: Its a value type whenever we pass it , its passed by value.like Struct and Enum, String itself a Struct in Swift.

public struct String { // string implementation }

But copy is not created when you pass. It creates copy when you first mutate it.

String is automatically bridged to Objective-C as NSString. If the Swift Standard Library does not have, you need import the Foundation framework to get access to methods defined by NSString.

Swift String is very powerful it has plethora of inbuilt functions.

Initialisation on String:

var emptyString = ""             // Empty (Mutable)let anotherString = String()     // empty String immutable    let a = String(false)           // from boolean: "false"let d = String(5.999)           //  "    Double "5.99"let e = String(555)             //  "     Int "555"// New in Swift 4.2 let hexString = String(278, radix: 18, uppercase: true) // "F8"

create String from repeating values:

 let repeatingString = String(repeating:"123", count:2) // "123123"

In Swift 4 -> Strings Are Collection Of Characters:

Now String is capable of performing all operations which anyone can perform on Collection type.

For more information please refer apple documents.


Your best bet is to use Swift native types and classes, as some others have noted NSString has toll free translation to String, however, they're not the same a 100%, take for example the following

var nsstring: NSString = "\U0001F496"var string: String = "\U0001F496"nsstring.lengthcount(string)

you need to use the method count() to count the characters in string, also note that nsstring.length returns 2, because it counts its length based on UTF16.

Similar, YESThe same, NO