Friday, April 13, 2018

Tuesday, April 10, 2018

Swift Data Types



                       




Data Types:
As we discussed in the previous lesson, about variable and variable we have seen that, when we try to assign the String to Int type it was showing error message cannot assign, Here what String and Int are?.

Let discuss then
As shown and described in the above image, different types of datatypes in Swift, commonly used. Reason why there are many datatypes? Is this because system stores different types of data differently.



As we see in above picture variable that we declared as 'str'  and it is declared as to store only data of type string 'Hello, playground test' by a system.

To declare the variable we basically use
var str = "my test home"  here str assumes it should hold data of type string


The other way to declare is 

Saturday, April 7, 2018

Swift Variables, Constants

Variables: In swift, a variable holds some data, now its data could be a value, it could be a reference to an object or it can even point to a function.

Variable has a couple of distinct parts, let's break it down, them here



var str = "Hello, playground"

Variable need to be declared before they can be used, so this 'var' keyword is used to declare a new variable, following the var keyword you have the variable name 'str' that's it.

Now taking look at the whole line, what we have on the right-hand side there, that's some data,  we didn't say that variable hold some data, Here = sign in between variable name and piece of data that mean assignment operator.  That means assigning that str with data.



In above line, the playground shows the error because the data is not matching.
Let's start learning about the data types.

Before starting data types, let's discuss briefly naming conventions, naming variable we usually follow the camelcase.


Constants: Like the variable except it can't be reassigned new data after the first assignment.
Uses the 'let' keyword instead




Why would I or everyone use constants, if it more limited it's in functionality, instead if I declare everything in the variable I have all the flexibility I want.

Well, there are a couple of reasons why you would want to use constants, when making sense, for one thing, it helps computer works little more efficiently because it knows exactly what that constant will be and that not going to change.

Next chapter Data Types