Swift Basics

This is not fair.

Age is an integer why are you declaring it as String you may ask.

Well, we have a solution around that, which we will discuss in our next article.

(Its fun to keep the suspense on).

Moving on, keeping the previous point in mind.

Few more important syntax you might wanna know.

Type AnnotationsYou can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store.

var myName : StringmyName = "Strongest Avenger"This is something useful where you can initialize kind of values your constants or variables require in your program later on.

CommentsWell, you should aim for writing self-readable code, but hell yeah comments are important.

/* this is the start of the commentThis is a multi-line commentdeclaring a string without type annotation*/ var myString = "Not a comment" //Single Line Commentlet age = 20let myName = "Thor" //Some people prefer thislet yourName = "Thanos" /* Some people prefer like This */Cut to the point.

// is used for a single line comment.

and for multiline comments, you enclose your lines between /* and */SemiColonIf you are familiar with other languages, you know what I am talking about,even if you are not it is okay to read about it.

Don’t use semicolons.

( Wait… What???) As you can find on Apple resource — it can be used for multiple statements in a single line.

But what about clear code?Try to avoid writing your code like this.

It won’t do justice to readability and then you need to use semicolons.

let x = 40.

0; let y = 20.

0; print("(x, y): ((x),(y)")Why not write to like this.

let x = 40.

0let y = 20.

0print("(x, y): ((x),(y)")It is more readable and without semicolons.

Ohh I almost forgot about writing Print Statements.

Printing Constants and VariablesYou can print the current value of a constant or variable with the print(_:separator:terminator:) function:let age = 27print("User age is: (age)")print("User age is: " + String(age))print(String(format: "User age is: %d", age))Well, I prefer the first way but if you know other programming languages, you can find it easy to use second and third way depending upon your choice.

Yeah, I know this is a long article but a couple of things I will talk about it here and then resume from next article.

Type AliasesA type alias allows you to provide a new name for an existing data type into your program.

After a type alias is declared, the aliased name can be used instead of the existing type throughout the program.

Type alias does not create new types.

They simply provide a new name to an existing type.

The main purpose of typealias is to make our code more readable, and clearer in context for human understanding.

It is declared using the keyword typealias as:typealias name = existing typefor example,typealias StudentName = Stringlet name:StudentName = "Jack"This will be useful for complex types we are gonna use later.

In Swift, you can use typealias for most types.

They can be either:Built-in types (for.

eg: String, Int) (You know about this)User-defined types (for.

e.

g: class, struct, enum) (Later in this series)Complex types (for e.

g: closures) (Later in this series)SummaryOh, you have been patient but glad you took your first steps, you learned a little about how programs put variables and constants in memory.

A little bit about different types.

It’s slow learning, but we are making progress.

There are a lot of tutorials out there that are much faster than I will be, but I want to make sure you understand the how and the why.

Thanks for reading this article.

Liked this article?You can give up to 50 claps: by long pressing the clap button.

if you enjoyed reading this article or learn something new, please tell your friends and share the love for this article.

What's Next?In the next article, we will go over operators and optionals and then some of the questions which I left unanswered here.

Most of the preliminary background information is over, and soon we will be having fun with programming.

Optionals, Operators in SwiftIn the previous article, we have talked about variables, constants, and types.

(not all of them).

In this article, we…medium.

comFor getting updates for interesting articles related to tech and programming to join TechShots.

Its a start to a long journey.

We will love the developers to be a part of this and publish blogs related to any tech they like.

You can also send us suggestions at techshotscommunity@gmail.

com.

Your feedback is very valuable.

TechshotsTechshots.

4 likes.

Educationwww.

facebook.

comReferences :https://docs.

swift.

org/swift-book/LanguageGuide/StringsAndCharacters.

htmlhttps://docs.

swift.

org/swift-book/LanguageGuide/CollectionTypes.

html.. More details

Leave a Reply