UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Why are strings structs in Swift?

Paul Hudson    @twostraws   

Updated for Xcode 15

In many other languages, strings aren’t structs – they are just a sequence of letters, and nothing more. Swift takes a different approach: strings are structs, and in fact come packed with functionality.

Almost all of Swift’s core types are implemented as structs, including strings, integers, arrays, dictionaries, and even Booleans. This isn’t an accident: structs are simple, fast, and efficient in Swift, which means we can create and destroy them as much as we need without worrying too much about performance.

However, strings are a particularly complicated problem to solve because language is complex. Yes, things like the English alphabet takes just 26 lowercase and 26 uppercase characters, but Swift also needs to be able to handle other scripts – Hebrew, Arabic, Cyrillic, Tamil, and many more. Heck, there are over 50,000 Chinese characters!

Where things get really complex is how we handle emoji, because there are countless variations for skin tone and gender. Rather than storing each emoji as a unique “letter”, these often broken down into multiple special letters. For example, the emoji “two women holding hands, where the the left woman has light skin tone and the right woman has medium-dark skin tone,” Swift actually stores:

  • Woman
  • Light skin tone
  • Joined to
  • Shaking hands
  • Joined to
  • Woman
  • Medium-dark skin tone

So, we actually use seven “letters” to store one emoji in that instance – it’s complicated!

As a result, Swift encapsulates all the functionality to handle strings into the string themselves. This means we can use functionality such as the count property without worrying about it miscounting emoji. The alternative would be to have hundreds of standalone functions to handle strings and their complexities, which wouldn’t be pleasant.

Now you understand some of the complexities of strings, take another look at these properties and methods:

print(string.count)
print(string.hasPrefix("Hello"))
print(string.uppercased())
print(string.sorted())

I hope you can see they do a heck of a lot of work on our behalf! Sorting a string with emoji would cause all sorts of problems if the individual characters inside the emoji were pulled apart. Fortunately, Swift’s strings are designed to take care of all this work for us, and although it causes a few road bumps here and there the overall benefit is massive.

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.