Swift Learning: The basic part (the Basics)

Source: Internet
Author: User

Swift is a new language for developing IOS MacOS WatchOS and TvOS apps. However, if you have a C or objective-c development experience, you will find that many of Swift's content is familiar to you.

Swift contains all the underlying data types on C and Objective-c, Int represents integer values, Double and float represent floating-point values, and bool is a Boolean, and String is text-type data. Swift also provides three basic collection types, Array, set and Dictionary, as described in collection types.

Like the C language, Swift uses variables to store and correlate values through variable names. In Swift, variables with immutable values are widely used, which are constants and are more powerful than the C language constants. In Swift, if the value you are dealing with does not need to change, the use of constants can make your code more secure and express your intentions more clearly.

In addition to our familiar types, Swift also increases the number of higher-order data types not found in objective-c such as tuples (tuple). Tuples allow you to create or pass a set of data, such as the return value of a function, you can use a tuple to return multiple values.

Swift also adds an optional (Optional) type to handle the case of missing values. Optional means "There is a value, and it is equal to X" or "There is no value". Optional is somewhat like using nil in objective-c, but it can be used on any type, not just a class. The optional type is more secure and expressive than the nil pointer in Objective-c, which is an important part of Swift's many powerful features.

Swift is a type-safe language, which means Swift can give you a clear idea of the type of value. If your code expects a String, type safety prevents you from accidentally passing in an Int. Similarly, if your code expects to get a string, type safety prevents you from accidentally passing in an optional string. Type safety can help you find and fix bugs early in the development phase.

Constants and variables

Constants and variables associate a name (such as maximumnumberofloginattempts or welcomemessage) with a value of a specified type (such as the number 10 or the string "Hello"). The value of a constant cannot be changed once it is set, and the value of the variable can be changed arbitrarily.

declaring constants and variables

Declare the constants with let, and declare the variables with var.

Let maximumnumberofloginattempts = 10

var currentloginattempt = 0

Declare a name that is Maximumnumberofloginattempts de new constant and give it a value of 10. Then, declare a variable whose name is Currentloginattempt and initialize its value to 0.

In this example, the maximum number of attempted logins allowed is declared as a constant, because this value does not change. The current number of attempts to log in is declared as a variable, because this value needs to be incremented each time an attempt to log on fails.

Can be in 1?? Declare multiple constants or variables in a row, separated by commas:

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

Type callout

When you declare a constant or variable, you can add a type callout (type annotation), indicating the type of the value to be stored in a constant or variable. If you are adding a type callout, you need to add a colon and a space after the constant or variable name, and then add the type name.

var welcomemessage:string

This variable can only store values of type String.

The colon in the declaration represents "Yes ... Type ", so this line of code can be understood as:

"Declares a type is a String, and the name is a variable of welcomemessage. ”

"Type is String" means "a value of any string type can be stored." ”

The Welcomemessage variable can now be set to any string:

Welcomemessage = "Hello"

You can define multiple variables of the same type in a row, separated by commas, and add type callouts after the last variable name.

var red, Green, blue:double

Attention:

Generally, you rarely need to write type callouts. If you assign an initial value when declaring a constant or variable, swift can infer the type of the constant or variable. In the above example, Welcomemessage is not assigned an initial value, so the type of the variable welcomemessage is specified by a type callout, not by the initial value.

  

Swift Learning: The basic part (the Basics)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.