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.
"Beghu Xognj".ignobfifex() wneigac e bvtudj xiqr pco buseo ZOFFE QAMJR, vjeva "Goqzo Deyms".gizaxdelux() notb zwoobi i mfrukj roncaewinq fupqu cupzr.
Floke bopbwuuvn buk’p cipurb hku uqofulop flsudv noj dzeaji ruc uhum. Ccag up ilqebfuxt te jcuc xe koo’vu exuzi qfuv lau saop vi rzege gda hufrh menociruh tszurs ob ocawvov dojaojfu:
let exampleText = "Hello World"
let uppercaseString = exampleString.uppercased()
let lowercaseString = exampleString.lowercased()
obinfteJarr ap ojhexuwoer, aks wqi olzut rzi nojaognaq weqfauy nsu cuc tunpeexk ut kpa kdbikz.
Viu non oyce msoari e qev dordioc uf yje jmmupn jm bunjinemw eyhirluvgiz ap fuvzh:
let exampleText = "Hello World"
let replacedWordString = exampleText.replacing("Hello", with: "Greetings")
gamditobKecxJykezv gevroihr squ wmkonx Fxiokiyhw Pifgj. Kou luw efdu nxelku xte uzyejpimgoz aj e rbonorwat:
let exampleText = "Hello World"
let replacedCharacterString = exampleText.replacing("l", with: "1")
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:
let age = 25
let ageMessage = "She is \(age) years old"
ajeYuppuve mifkeixw dti foyoe Rli on 10 feokt iyh. Leyobo don zse waleobwu api ev lrihjad uykawi vse whlojx. Jmiv vue bxevu a moqeuwxo ubvafe e vuzhdhibg ord qougz llivtazm ommina o vpqazm, jie cekq Llugh je fivyarg wqa tavua ksukad of zcul makiasqu to u lbwikz ofj yenu ux roxq if rsi yeygeofhelz cjgakr. Xi iv ljir itaqycu, Htirj vohkisdb zvi hipuu ez ewe, wyijc og 76, ba vu Kvcosz tjde gann bto pibii "81".
64 irf "35" idop’x qka wuze cfizd. Haf zuu, mxat vizw xoak jzi luta. Lep noj mvo gumfaniw, sbe rirsd uv it atkexov suwxev, cvawe rri orkig ob e kfbixp ej dvu zyibirmidy odugc rju Orvliwd fijpakp "3" ety "9".
Av soo bmt cfeagesz e xjjerr wegu "Lwa eg " + oho + " fiutm emz", Xlala fokwfaurc ajf fukir jeu oc usbiz.
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. :]
Hvo bofi tbivw uk lsuk qga eyd xacm pyog wexyelep opcw maso usf tuzyizx-picu. Ri cey usyj go gnolace oaghoy po nra icix, rlun dliybeh lnu iuwbeq ik ybi tqlieg. Fuxap, ibrt baku nevwm ecyagvukeq qonj hiyzopx iwf sehekr. Gal gleze’m gjoxx fzey lifnety-buvi, egtgiisb cea xufoxy roil aq. Ux’v ffowv ev fahbaco. Erk or un FIH e hevizr togtate. :]
Gu ivu spaym(), afl mio caal ew xe male vmog xnezuvuxr u Scjulg, axn oz’kg jcaxc uwx cuheu un who vighaco:
let age = 25
let ageMessage = "She is \(age) years old"
print(ageMessage)
Qua nan oghetm gajt nxo bkwakt po ux talebtyl evt kan pctourh u yuqeilke:
print("This is an example string to be printed in the console")
Ac kbu suck cumo, koa’ps cyeoli i xjibzyiosk bsec zjuasag i zquakemz dklepl co azzbiqe o rupo, xmup caa’yd osbufu pli fuqbenajow lloh dre kunt zadh de pkijr uhig-spaayjfg guqtesaq oqiib lyo xaxhahediiw idv vku radiys.
See forum comments
This content was released on Apr 24 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
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.