Swift supports operator overloading, which is a fancy way of saying that what an operator does depends on the values you use it with. For example, +
sums integers like this:
let meaningOfLife = 42
let doubleMeaning = 42 + 42
But +
also joins strings, like this:
let fakers = "Fakers gonna "
let action = fakers + "fake"
You can even use +
to join arrays, like this:
let firstHalf = ["John", "Paul"]
let secondHalf = ["George", "Ringo"]
let beatles = firstHalf + secondHalf
Remember, Swift is a type-safe language, which means it won’t let you mix types. For example, you can’t add an integer to a string because it doesn’t make any sense.
SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.