Allright, solet's getstartedfortherestofthelessonsintheSiri's, we'regoingtobeworkinginsidethisappcalled X Code X CodeisthetoolthatApplehasprovidedforpeopletobuildAPSwithandinside X code.
YoucanseeherethatwhenyouwanttocreateanappforiPhone, iPad, Macer, whateveryou'regoingtogotocreate a new X Codeproject, andthat's goingtostart a fulloutprojectforyoutobuild a napin.
Buthere, we'regoingtostart a newplaygroundbecausethat's goingtoallowustotypesomecodequickly.
It's gonnaletusrunthatcodeandseetheresults, andwe'renotcommittingtobuildingin a project.
It's simplylike a sandboxor a playground, asit's named.
Now, ifyoudon't seethiswelcomescreen, whatyoucandoisjustgouptofileNeilandhitPlaygroundrightthere, andthat's goingtostart a newplaygroundforyouaswell.
Oryoucouldjusthitthisbutton, getstartedwiththeplayground, sothat's gonnalaunchthisdialogueforyou, and I'm simplygoingtoleavethedefaultnametheirplatformshouldbeIowa's, and I'm goingtosavethisonmydeskcop.
Okay, soafteryourplaygroundruns, you'llseeuphere, it's doing a wholebunchofstuff.
Okay, we'regonnastartwith a quicktourofthisplaygroundwindowhere.
Sodownonthelefthandside, I have a bunchoflinenumbers.
Now, ifyoudon't havetheseandyouwantthem, theycanbeusefulattimes, especiallywhen I'm teachingand I needtorefertolinenumbers.
Youcangotoe X codepreferences, andyoucangototextediting, andyoucansimplyenablelinenumbersthere.
Now, thisarearighthereiswherewe'regoingtotypeourswiftcode, andit's alreadygot a bunchofstuffforusrighthere, whichwe'regoingtogothroughin a secondontherighthandside, you'regoingtosee a quickpreviewoftheresultsofwhatyoutypethere.
Now, a lotofpeoplehavetroublewithgettingoutputtoshowuponthisrighthandsidebasedonwhattheytypehere.
Sowhatwe'redoinginthislineofcodeiswe'redeclaring a variablecalledstrandthen a signingthatpieceofdatatothatvariable.
Sonowjumpingbackintotheplayground, let's seetheeffectofthat I cansaysomethinglikeprint, whichis a keywordthatisgoingtoprintsomethingtothatconsoleareadownbelow.
Soif I sayprintandthenintheparentheses I putstryoucanseethatit's printedthathello?
Playout s roughlyingdownthereontherighthandside.
Wecansee a quickpreviewofthatandnoticethatthere's a slashendhere.
Andthatisbasically, um, justdenotingthatthere's a newlineassociatedwiththatprintstatementbecause, ah, thenexttime I printsomething, it's gonnabeprintedonthenextline.
Soyoudon't seethatherebecause a newlineisjust, um, a carriagereturnorwhitespace.
Youcan't reallyseeit, butinhereinthispreview, it's visualizedasthatslashandwhichisjust a newline.
Solet's take a lookat a coupleofdifferentpiecesofdatathatwecanstoreintovariables.
So I'm goingtodeclareanothervariableherecalled a, and I'm goingtoassign a numberintoit.
Sojustbaroneand I meanitclearanotheronecalled B, which I'm also a signone, and I wanttoshowyouthatyoucanperformoperationsbetweenthevariablesoronthevariables.
So, forexample, I cansaveprint A plus B, andyoucanseeherethattheresultintheconsulhereistonownoticethatwhat I'm usingthevariablesinanequationoranoperationlikethis I don't needtousethekeywordfar, becausethat's onlyfordeclaringvariables, right?
Furthermore, youcanalso a signvariableasanothervariableso I cansay a equals B downhereandinthatcase, a woodnowequaloneagain.
Well, itwasonetobeginwith, SoSolet's say I didthat.
Soifitwastooandthen I said a equals B, thennowtheywouldbothbeone.
Now, beforewemoveon a quicknoteonnamingvariablesHerewehave a variablestrand I'venamedsomevariables A and B sonormalyouwantnamevariableslike A and B becausethey'renotdescriptiveofthedatathattheyhold.
Soifyou'regoingtobeusingvariablesjustlikethisinanoperation, whenitbeeasierifyouknewwhat a representedinoneberepresentedwhileyoucandeclareyourvariablesusinganysortsofnamesthatyou'd wantbutyoucan't putsomespecialcharactersandyoucan't putspacesinyourvariablename.
However, thecommonpracticeistouse a formofcamelcasewherethefirstletterislowercase.
Soletmeshowyouwhat I mean, so I cansaveourAndif I wanttodescribemyvaluableifitrepresents, umlet's saythenumberofapplesthat I have.
I wouldsaysomethinglikemynumberofappleslikethat, andyoucanseethateachnewletterofeachwordiscapitalizedexceptthe 1st 1 Nowyoudon't wanttogettoocrazywiththelengthofthevariablenameaswell.
Andsowhenwegobacktotheplaygroundandwetake a lookatthisvariablethatwedeclaredherstranditsstores a string, thesystemhasallocatedthisvariabletostorethispieceoftextorstring.
Thenwhatit's gonnadoisassoonasyouassign a pieceofdataintothatvariable, it's goingtotake a lookatwhattypeofdatathatisandassumesthatthatvariablestoresthatdatatype.
Sonow, goingbackintotheplaygroundhere, it's asifwedeclaredthis s t r wearablelikethis, right?
Andtheseonesarelikethisnow, forinstance, if I declaredthisstrvariablelikethis, thenthislinewouldbeanerrorbecause I'm tryingtoassign a stringinto a variablethatisoftypeint, Soit's notgoingtolikethat.
Nowlet's changethisbackto a stringfor a secondandgobacktothislinewherethere's anerror.
Nowyouunderstandwhythislineisinredbecausewecan't assignaninninto a stringvariable.
Whiletherearewaystoconvertdatafromtypetotype, insomecasesitmakessense, like 29 here.
Thiscanberepresentedas a pieceoftextsimplylikethat, right?
Butconversely, thisHelloplaygroundlinerighthere.
I can't convertthattoanintegerbecauseitdoesn't makesensethatthismessagecouldberepresentedby a number.
So, forexample, justthatis a littlepreviewhere.
If I wantedtoconvertthisnumberorthisintegerinto a string, I wouldcreateanystringandpassinthenumberlikethat.
Butfornow, inthislesson, I wantyoutounderstandthattherearedifferentdatatypes, whythey'redifferentdatatypesandhowthatcomesintoeffectwhenyou'redeclaringyourvariablesandworkingwithyourvariablesinyourdata.
Now, beforeweendthislessonoff, letmejustshowyou a coupleofexamplesoftheotherdatatypeswetalkedabout.
Sofloatcouldbesomethinglikethat.
A doubleusuallyalsolookslikethis, andyou'renotgonnahave a reallylargedecimalpointunlessit's like a resultof a calculationthatyou'redoing.
Um, andletmeshowyou a Booleanwhichisrepresentedasbull.
So, forexample, if I have, let's declare a constantheresothat a equals 10 forexample, and I wantedtoprintthisstatement, um, onLee, if a islessthan, let's say 11.
Whichitisrighthere.
Butmmm.
Okay, actually, thatisThatisnotright.
Let's sayfour.
Andthisgivesus a reasontousethe F statement.
Right?
Okay.
Soprintonlyif a islessthanfour, butis 10 right?
Soweshouldn't beoutputtingthissowecanusehereisanifstatementandwhatitallowsustodoisonLeeexecutesomecodeif a certainconditionistrue.
Sointhiscase, wewanttoprintthisonLee.
If a islessthanfour, sowecanwriteanifstatementtocheckifthat's trueornot, beforeweprintthatstatement.
SoitallstartswiththeifkeywordfollowingtheIfkeyword, wehavetheconditioninwhichwewanttocheckandfollowingtheconditionwehave a setofcurlybracketsnowinsidethecurlybrackets.
If a islessthanfoursowecandosomethinglikethatif a andthenwecanusethisoperatorlessthanfourandremember, weneedtosurroundthepieceofcodethatwewanttoexecute.
Iftheconditionistrue, using a pairofcurlybracketslikethatsoyoucanputthecurlybracketonthesamelineoryoucanputiton a secondline, itdoesn't matter.
Let's startbycreating a newplaygroundforthisexample, and I'm goingtostartlabelingthesafterthelessonshere, and I shouldhavedonethissooner.
But I wanttopointoutthatAppledoeshave a swiftprogramminglanguageguideandinthisguythatgoesthroughallofthesyntaxandthestructureinthedifferentconceptsoftheswiftprogramminglanguage.
Andthenyouopenup a pairofcurlybracketsinsidethesecurlybrackets, Youhavedifferentcaselabels.
Soyouwouldhavecaseifyouwantedtoconsiderthecasewherethevalueisan A youwouldhavecase a andthenyouhavecolonandyou'd havesomethingtodoIfitwerethatcase, perhapsthiswouldbeeasiertovisualizeifwetake a lookattheplayground.
So, forexample, if I changethistosayisbeoryouknow s e is a b or C.
I coulddosomethinglikethiswiththiscaselabelhere.
I candothatcomma.
Andthen I canput, um, thisothercasethere, Sothissavesmefromhavingtohaveanothercase, see?
Andthenyouknowthesamecode.
I canput a commahere, puttheothercasehere, andhaveeitheroneofthesecasesusethesameblockofcode.
Sonowyoucanseethatitcomesintothiscasehere.
Theprinceis a, B or C insteadofcomingintodefault.
Sowhywouldyouwanttouse a switchstatementoveranifstatement?
Well, ifthereare a lotofdifferentconditionsthatyouwanttocheckon a certainvalueratherthanusinganifstatementwithahHoltonofelseifs, youcanusethisswitchstatement, whichiseasiertoreadandeasiertounderstand.
You'reprobablygoingtousetheifstatementmorethantheswitchstatement, butit's still a handytooltohaveinyourarsenal.
We'regonnastartwiththefourinLoop, asyoucanseeintheSwiftProgrammingguidehere, solet's goaheadandstart a newplaygroundinourexcode, and I'm justgoingtocallthistheLoopplaygroundandsaveitonmydesktop.
NowfourinLoopallowsyoutorepeat a pieceofcode a certainspecifiednumberoftimesIfyouhave a listofdataotherwiseknownasanarray, whichyou'regoingtolearnaboutinanupcominglesson, youcanalsouse a fourinLuketogothrougheachofthosepiecesofdatainthatarrayandexecutesomesortofcodeonit.
Soyoustartwiththekeywordfourandnextyouhave a variablename.
Nowyoudon't havetodeclarethisvariableusingthevarcuredLikeyou'velearnedinthepreviouslessons, youcansimplyhave a nameforthisvariablebecauseyou'reonlygoingtorefertothisvariablewithintheforeignloop.
FivetimesWedon't reallyneed, um, a counterofanysort.
Wedon't needtorefertoindex.
Wedon't carewhichiterationit's currentlyat.
Onething I wanttomention, whichisconfusingoftenforbeginnersisthisideaofscopehere.
Forexample, if I wantedtosumupthenumbersfrom 1 to 5 and I wantedtoprintouttheresultafterthefourloop, let's say I declare a variableappearand I callitsomenightequatedtozero.
Andthen I saysomethinglikesomeplusequals.
Andactually, let's addthisindexvariablebackhereand I golikethis.
Okay, sofirstofall, if I declarethisvariablecalledsomeinsidemyfourloopinsidethesecurlybrackets, whilethisvariableisonlyavailablewithinthatscopewithinthesetwocurlybrackets.
I can't goouthereoutsideofthefourloopafteritsrunfivetimesandprintouttheresultofsome.
See, youcanseethatitcan't findthisvariableeventhough I'vedeclareditinhere.
And I wouldsaythat's true, exceptthatineachiterationintheloop, it's almostasifitdoesn't rememberwhathappenedinthepreviousiteration.
There's nomemoryorrecollectionofthepreviousgeneration, soeachiterationiskindoflike a cleanslate, andit's gonnaexecutethiscoderighthere.
Sojusttosaythatagain, you'reright.
Ifyou'resayingthat I can't readeclairsome, allright, becauseif I triedtodothatrighthere, excludewouldthrowinerror.
However, insideof a loopinthisscoperighthere, eachiterationoftheloopislike a cleanslate.
Okay, now, if I wantedtoactuallyfindoutthesomeofthenumbersfrom 1 to 5, what I wouldactuallydois I wouldmovethisdeclarationuphereoutsideoftheloop, andthenwhat I woulddoThisway, I couldmovethisprintstatementoutsideoftheloop.
Andwhatthisforeignloopwilldoissimplyloopfrom 1 to 5 eachtimeaddingindextothesum.
Andnow I'm actuallykeepingtrackofthesum.
Soyoucanseethatafterfiveitorationsoftheloopaddingthenumbersfrom 1 to 5 intosome, whichstartedoutzero.