Swift language the basics
Basic Introduction to Swift Language

Swift language the basics

Work with common kinds of data and write basic syntax.

Disclaimer: This is official documentation article on Swift language with some minor addition.

Swift is a programming language for iOS, macOS, watchOS, and tvOS app development. If you have experience developing in C or Objective-C, many parts of Swift will be familiar to you.

Swift provides its own versions of all fundamental C and Objective-C types, including Int for integers, Double and Float for floating-point values, Bool for Boolean values, and String for textual data. Swift also provides powerful versions of the three primary collection types, Array, Set, and Dictionary, as described in Collection Types.

Like C, Swift uses variables to store and refer to values by an identifying name. Swift also makes extensive use of variables whose values can’t be changed. These are known as constants, and are much more powerful than constants in C. Constants are used throughout Swift to make code safer and clearer in intent when you work with values that don’t need to change.

In addition to familiar types, Swift introduces advanced types not found in Objective-C, such as tuples. Tuples enable you to create and pass around groupings of values. You can use a tuple to return multiple values from a function as a single compound value.

Swift also introduces optional types, which handle the absence of a value. Optionals say either “there is a value, and it equals x” or “there isn’t a value at all”.

Swift is a type-safe language, which means the language helps you to be clear about the types of values your code can work with. If part of your code requires a String, type safety prevents you from passing it an Int by mistake. Likewise, type safety prevents you from accidentally passing an optional String to a piece of code that requires a non-optional String. Type safety helps you catch and fix errors as early as possible in the development process.

Constants and Variables

Constants and variables associate a name (such as maximumNumberOfLoginAttempts or welcomeMessage) with a value of a particular type (such as the number 10 or the string "Hello"). The value of a constant can’t be changed once it’s set, whereas a variable can be set to a different value in the future.

Declaring variable in Swift language


Declaring Constants and Variables

Constants and variables must be declared before they’re used. You declare constants with the let keyword and variables with the var keyword. Here’s an example of how constants and variables can be used to track the number of login attempts a user has made:

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0        

This code can be read as:

“Declare a new constant called maximumNumberOfLoginAttempts, and give it a value of 10. Then, declare a new variable called currentLoginAttempt, and give it an initial value of 0.”

In this example, the maximum number of allowed login attempts is declared as a constant, because the maximum value never changes. The current login attempt counter is declared as a variable, because this value must be incremented after each failed login attempt.

You can declare multiple constants or multiple variables on a single line, separated by commas:

var x = 0.0, y = 0.0, z = 0.0        

Note

If a stored value in your code won’t change, always declare it as a constant with the let keyword. Use variables only for storing values that need to be able to change.


要查看或添加评论,请登录

Masab Ejaz的更多文章

  • Basic Types in Kotlin Programming

    Basic Types in Kotlin Programming

    Every variable and data structure in Kotlin has a data type. Data types are important because they tell the compiler…

    2 条评论
  • Welcome to Kotlin Programming!

    Welcome to Kotlin Programming!

    In this article, the fundamentals of the Kotlin programming language can be completed entirely within your browser…

  • Synchronous vs. Asynchronous Programming: What's the Difference?

    Synchronous vs. Asynchronous Programming: What's the Difference?

    Synchronous and asynchronous are two distinct programming styles, each with its advantages and disadvantages. You may…

  • How integers are stored in memory

    How integers are stored in memory

    In most modern computer systems, both positive and negative integers are stored in memory using a binary…

社区洞察

其他会员也浏览了