NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Why are strings structs in Swift?

Paul Hudson    @twostraws   

Updated for Xcode 12.0

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 Fernando Olivares

SPONSORED Ever ask for help and your reviewer immediately notices issues you missed? Fernando Olivares is a 10-year veteran from Big Nerd Ranch and Lambda School who can help hone your app building skills, and he's just launched a new book that does just that – use the code "hacking" to save $5!

Buy the book

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

BUY OUR BOOKS
Buy Pro Swift 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 (Vapor Edition) 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 Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

Link copied to your pasteboard.