We learned in the last lesson that we need the var keyword to declare new variables, or the let keyword for new constants: var firstName = "Tom"īut whether you’re declaring a variable or a constant, there is, in fact, an optional part of the declaration we left out.
#Swift handy calculator subtraction how to#
How to Specify the Data Type for your Variable or Constant However, they are the most common data types you will work with for a while. They are perfect for when there are only one of two options.īy no means are these the only data types in Swift.
#Swift handy calculator subtraction code#
To start, let’s look at a line of code from the previous chapter: var str = "Hello, playground"Ībove, we’ve declared a variable called str, and assigned a piece of string or text data to it. In this chapter, we’ll talk about different data types in Swift.
![swift handy calculator subtraction swift handy calculator subtraction](https://i.ytimg.com/vi/QsLHrEUCvVQ/maxresdefault.jpg)
There’s a fine balance to upkeep when it comes to the length of your names. I mentioned before that your variable names should be descriptive so you know what kind of data they hold. Best Practices for Naming Variables and Constants In those cases, it’s simply a matter of changing the var keyword to the let keyword in the variables’ declarations. Also, if your program uses variables that never change their data, Xcode will suggest changing the variables into constants instead. Over the course of your Swift journey, you’ll build a sense of when to use variables versus constants. Despite this, it’s more preferable in some cases to use constants over variables, like to keep track of data you don’t intend to change later.
![swift handy calculator subtraction swift handy calculator subtraction](http://www.tcocd.de/Pictures/Mechanical/Swift/handy-1.jpg)
![swift handy calculator subtraction swift handy calculator subtraction](https://dygtyjqp7pi0m.cloudfront.net/i/24931/22626275_3.jpg)
Here, Xcode tells me that I cannot assign something else to the constant lastName because it was already assigned the value of “Smith”.Īt this point, it seems that variables are much more flexible than constants. Here’s how our variable looks in the playground: A complete example of the variable declaration is given below. Since we named this variable “firstName,” we’ll give it a piece of text data, namely the word “Tom.” Note: the single equal sign lets us assign a piece of data to the firstName variable. For example, if I had a piece of data for someone’s first name, I might name my variable firstName.įollowing your variable name, add an equal sign and then the piece of data you want to keep track of. You can name it anything you want, but the name should clearly describe the data the variable will contain. First, type the keyword var, followed by a space, and your variable’s name. Let’s learn how to create, or declare, a new variable to track a piece of data in memory. This is where variables come in, to help keep track of data within an app. For us to work with data, we need a way to refer to it in our programs. Your computer stores data in its memory to keep track of where the data is and what it is.
![swift handy calculator subtraction swift handy calculator subtraction](https://img0.etsystatic.com/036/0/8516689/il_fullxfull.549450366_9bs2.jpg)
We can even have text data like "Hello" surrounded by double quotes on line 5. Data can be numbers like 123 and the decimal 0.7 as shown on lines 3 and 4.