Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - which types to use? NSString or String

With the introduction of Swift I've been trying to get my head round the new language

I'm an iOS developer and would use types such as NSString, NSInteger, NSDictionary in an application. I've noticed that in the "The Swift Programming Language" ebook by Apple, they use the Swift types String, Int, Dictionary

I've noticed the Swift types don't have (or are differently named) some of the functions that the Foundation types do. For example NSString has a length property. But I've not been able to find a similar one for the Swift String.

I'm wondering, for an iOS application should I still be using the Foundation types?

like image 363
Alec Avatar asked Jun 04 '14 13:06

Alec


People also ask

What is difference between string and NSString in Swift?

The Swift string is one character long, as expected. The NSString says it has a length of seven — this matches with the length of the Swift string's utf16 view, since NSStrings are backed by UTF-16: 09:02 The Swift string's unicodeScalars view returns a count of four.

Is string a collection in Swift?

A string is a series of characters, such as "Swift" , that forms a collection.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

Is string mutable in Swift?

Strings can be created to be mutable or immutable using the var or let keyword respectively. Mutable strings can be changed after they are declared, whereas this is not possible in immutable strings as they are constants.


2 Answers

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

like image 96
Cezar Avatar answered Oct 18 '22 03:10

Cezar


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.

like image 42
Sandy Rawat Avatar answered Oct 18 '22 01:10

Sandy Rawat