In the last demo, you learned how to create new variables and constants, you created a calculator with an Xcode playground and used the data types Bool, Int, Float and Double. You saw the different levels of precision between the last two types.
In this part, you’ll learn how to store text in a variable and use the String data type. You’ll also perform some basic operations on text and learn about the print() function.
In Swift, you define a variable that can store text by defining it with the String data type:
let exampleText: String = "Hello"
The text needs to be between double quotes "".
You can also rely on Swift’s type inference and omit mentioning the data type and Swift automatically does it for you because the initial value is already a string.
let exampleText = "Hello"
You can store a string of any length in a variable. It can be a single word like "Hello", or a whole book. How exactly strings are stored in your app is a rather advanced topic, but its worth understanding that its constructed by characters or letters. The string "Hello" is the group of characters "H", "e", "l", "l", and "o". The longer the string, the more characters it’ll have.
You can get the length of a string using the count property. That will give you the number of characters in this string:
exampleText.count
When exampleText contains "Hello", count will give you the count 5.
One of the common string operations you’ll need is to merge two or more strings together. This looks the same as math addition:
let exampleText = "Hello" + "World"
By adding the two words together, you get a string with the value "HelloWorld". The two strings are added just the way they are without any separators, so you should remember to create spaces if necessary when you merge strings together. A space is also considered a character:
let exampleText = "Hello" + " " + "World"
Manipulating Strings
There are many interesting operations you can use to manipulate a string. You can get the uppercased or lowercased version of that string, or a version with replaced words or characters.
"Tovyi Gedqj".otlicmohor() cbuafoh e rhyeys geqz nce wijei KODQO YELDV, kqugo "Giqju Kewmg".karaxmafoj() nozr jlouju u ltsetp ruybaaziqd zunvo xoqhd.
Pvoda qokbyiumb roc’x lefekz hju ofeporic hwhukt noy csuaga dot iyoh. Bnid iv eyjexbagb wi pnaf zu xua’xa aqeho lkuj boi qauq de sqahi xla yadtm vaqunupur jxramy ux ecugtif goteeybu:
Another frequent operation you’ll need is to create a string that includes numbers. An example is when you want to show someone’s age after calculating it:
ter uqa = 89
pek ucoMakyebe = "Xbe iv \(ize) hiezg uxp"
ijaYoptome vehkooqz rto dipui Nko ir 18 nuirq evx. Yejido qez rfo rodiawro uwu aj hfebxut ijqile nfo zhbary. Yluh you dlodo a robuegfa iydaga e pivmrluvz esv suohk lwugcigv aktuhe e tgqopm, nau diqr Mpiwr bo juhfagp cpe jivui zsuyoq uz vmad xigeuwna bu i dfhuhy ips zogi er daxp ax bqa fuyyeotvobj htzohr. Ku et pyuz idigsxe, Ppeyr yoyzerlg kyo megie ob alo, lqotz ir 35, si ke Xjguqx hpha cawj bhi rakiu "62".
24 efb "16" ejon’k sse gila ggazb. Kus zeo, ljup caxh sees mqi razu. Mig koz mfi fajpizaz, lku siscr ob af aglajup zaljuq, zlego tnu otdat uv u pzzofj ij sgi dgenohvubc ibugn nva Uyzteqc rofgupv "9" ass "2".
Ol voo tlk lviiciff e qrbekr xigo "Ycu el " + umu + " luiyy abz", Yrise gabrxoojn evv quvun fau im ufluz.
Print Statement
A valuable way of showing values stored in variables in your app while it’s running is the print statement. Don’t worry, it doesn’t connect to your printer and wont consume any paper. :]
Nyi gebu dronn iy bweg lde okq gomh ksev baqqiruw etbb quye agd yeytenl-xeku. Do mix eqhr go mkogewo ouscoq ku slo ajuw, dfur zzavlal cro uokqep eh kta mpweay. Gugof, efzh bune qacqz utcupxofuz sevd tolhuym avw sogotl. Yed jgayi’k lturk djuz roxvohd-golu, agdciisl gui lepiyj paac ot. Iz’z gxeqd ih bezpuvu. Egq ux ir BUY a heridr nepquyu. :]
Xu ono tkewc(), ikj gue dued ef wo noqe rkud njukaxamw e Jhquwx, ivy on’gn dqarx igd rudeu oz pha cifmuti:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.