0

I have been reading Apple developer document that String is a value type and NSString (Foundation) is a reference type, similarly struct is a value type but if I use NSString inside struct it's working like a String. Can you elaborate the difference?

Example:

struct testing {

    static var testingstring1 = NSString()

    static var testingstring2 = String()

}
Catalina T.
  • 3,456
  • 19
  • 29
sweety S
  • 31
  • 1
  • 3
  • Possible duplicate of [structure vs class in swift language](https://stackoverflow.com/questions/24217586/structure-vs-class-in-swift-language) – Tj3n Jul 21 '17 at 11:09
  • NSString is class and String is struct – Saurabh Jain Jul 21 '17 at 11:12
  • NSString is class and String is struct , I understand but NSString is an reference type ,how it is working inside struct. – sweety S Jul 21 '17 at 11:15
  • Hi Tj3n..it is not a duplicate question.Please understand my question.I did't ask the difference between structure and class.I'm asking the difference between data type declaration inside the structure . – sweety S Jul 24 '17 at 05:51

4 Answers4

1

String is a struct

// in Swift Module

public struct String

{

}

NSString is a class

// in Foundation Module

open class NSString : NSObject

{

}

kashish makkar
  • 161
  • 2
  • 5
0

Swift is interoperatable with Objective-C and converts some Objective-C types to Swift types.

Types that can be converted between Obj-C and Swift are known as bridged types. String and NSString are example of such bridged types and hence you can assign NSString to a String variable.

Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33
0

String-NSString Mismatch:

The NSString conception involves UTF-16 code‐ points. Each approach has its advantages. The NSString way makes for great speed and efficiency in comparison to Swift, which must walk the string to investigate how the characters are constructed; but the Swift way gives what you would intuitively think of as the right answer. To emphasize this difference, a nonliteral Swift string has no length property; its analog to an NSString’s length is the count of its utf16 property. Fortunately, the element mismatch doesn’t arise very often in practice; but it can arise. Here’s a good test case:

let s = "Ha\u{030A}kon"
print(s.characters.count) // 5
let length = (s as NSString).length // or: s.utf16.count
print(length) // 6

We’ve created our string (the Norwegian name Håkon) using a Unicode code point that combines with the previous code point to form a character with a ring over it. Swift walks the whole string, so it normalizes the combination and reports five characters. Cocoa just sees at a glance that this string contains six 16-bit codepoints.

Saurabh Jain
  • 1,688
  • 14
  • 28
0

I think the issue is when you initialize the NSString, Swift is automatically changing it to a String. Try initializing the NSString like this and seeing if that gives you compile error:

    var testingstring1: NSString = "testingstring1" as NSString

Here is an explanation of the bridging between the two languages in terms of Objective-C's reference types and Swift's value types: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

ɯɐɹʞ
  • 1,040
  • 11
  • 17