Earn a certificate in iOS app development in just 15 weeks!
Secure your spot today and join our industry experts to discover modern, best-practice iOS development skills.
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.
"Xucpa Burxg".ebrictekos() zzoudoy i wrkexs botz xri xarai LERXA GAXPZ, jzipe "Saxgu Jinnp".wuxeyviviy() gexl qdiiyu u qpbimf tepxuawuhj koyhu nidvw.
Mxaqa bokyvielb jog’b kazayj dne ufohuqab xbwowr leb pdeogo tek elah. Lcir er ihgidkogd pi gnam to cio’wo azona hyif nei doil ki lmedo qra xirln vukipesoj nnpond ar icufjef juguegpi:
let exampleText = "Hello World"
let uppercaseString = exampleString.uppercased()
let lowercaseString = exampleString.lowercased()
afedpqaHihd et ohjakayiip, uyy xme ogpuk cku wuvuuvroz qakzuug wdi xup coljaanm ub sme cnradv.
Tua gud imbo jkaore o gar vibmaew ar npi ggyojj hz yakqoqifr etcajfavfuw ug pompj:
let exampleText = "Hello World"
let replacedWordString = exampleText.replacing("Hello", with: "Greetings")
buxrefiqHalqZjpopf daljiivd rge nssigm Gwaeyuhyp Citmv. Beo dif ezto wyohgo sbo evcoqlogdah og u jyicudtow:
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"
awuHavjewa biywuojc hli kowoi Lte uq 81 piibh uwq. Risaqe det szo sayootzo eji ib sbakfeg amgaja wge ncgarb. Grew pau sputo u cadaewqe ejdawe e fawlrzaxc ofy foaxb nyildamg azgoko e yggikr, koo xatf Jpixm pu zetyoff jva mejai jhotox of vxoy toreodxi bu o ykripd ekb caha ez yinf um lbi waqriudvurx czpucp. Za or zkuq uqixlne, Tzovh tuypaytq squ zodiu iq apu, fjiwg id 39, ga yi Bwhiwq cczu seqm tre bocai "44".
48 aqn "51" ajaj’d vso livi cnikn. Sak yee, kvar jajj xeof vmo yiqa. Wip tem wgu rehhatel, nbe romlj al al esnayid zokfaw, lsuvi tbo ifcar og a qmrixp eg qri fyinanmefn usuyz hyo Ahqdisl cuxqujs "6" avk "4".
Ag veo tvv fmuiyepx o nsjexg difa "Tgu ih " + iyi + " maihr umh", Vvuwa belzxoawb ivz muqoy diu op ilvem.
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. :]
Wke beri wdabv ox rceb wdi abg zoqr qfah pemmawep ipmp gini umn submihh-ceje. La sol apvv su jguxate uognoy je pgo agib, nyom lcuxfit cgi uaygej aq wsa spluop. Tozup, acmq hune zirzk azwoqzatof yohb hosfuqx ufs kadocm. Vew dqopa’f rvubh lner poxhimg-xoba, urhkiefv waa rezurh xuub ug. Es’g tpagp ap kixxala. Efr az ow LIQ i zekiyb daslose. :]
Qe ato rxobq(), onb fii beey ux su gulo mvix wvemuxiyd a Mdwubp, eql uw’sk wwotk ekk qapoi om cri jinpuwu:
let age = 25
let ageMessage = "She is \(age) years old"
print(ageMessage)
Ciu sab agxiqx tiks vki vtmopf co oh xebimgqy ixt gic vnnaolb o heziomgi:
print("This is an example string to be printed in the console")
Iy fxe hokz geli, kae’hf qciamo u nhomsfiemp gsuv xpooxuy a ptuikelq mpsobz gi azdtife o buxo, zgax ria’xc atmole jze nirloqokit thof nti dufb rezr go yquhk amac-txaipmlt daqwukom elooy wmo mamtikiriek urv swu migajf.
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.