Swift Signs and constants

Source: Internet
Author: User



Apple launched Swift also for some time, the internet also appeared a lot of information, very grateful to the great gods.



Swift's syntax differs greatly from the OC syntax, in which we create a class that generates both an. h file and a. m file, and only one. Swift file in Swift.



Grammatical differences also want to be large, such as constants and variables:





var a = 1

With Var, a is a variable name, 1 is a value, and if you need to change the value of a, you only need to:







A = 2

Note In Swift's language, there is no need to write ";" The





Swift has type inference, A is actually an int type, but we can omit it or omit it:





var a:int = 1a = 2

VAR is used to declare a variable, and if you need to declare a constant, you need to declare it with let:







Let B = 3

b is a variable, and you can't change its value at this time.








The name of Swift also no longer conforms to the identifier specification, the name can be encoded in Unicode, that is, when we declare a variable or constant, you can use Chinese as the variable name.





var Chinese = "China"

"China" is a variable name, the program will not error.








Type conversions:


var interValue = 10
var doubleValue = 9.0
doubleValue = Double (interValue)
println (doubleValue)

Swift does not support implicit type conversions. It is also for security reasons, so you need to cast the type.
Casting requires adding the converted type before the value being converted, and then adding parentheses, as in the code above:


Tuple:

Tuples combine multiple values into a composite value. The values in the tuple can be of any type, and are not required to be of the same type.
such as:

("age", 18)
This is a tuple, and the values in it can be of any type and in any order;
The value of a tuple can be broken down into separate variables / constants for use:

let stu = ("age", 18)
println (stu.0)
println (stu.1)

The background output at this time is:
stu.0 represents the first value of the tuple, stu.1 represents the second value of the tuple, and the subscript starts from 0;

We can add a label to each tuple, so that we can use the label directly when using:

let stu = (a: "age", b: 18)
println (stu.a)
println (stu.b)

The output at this time does not change;

If we only need to use a part of the value of the tuple, we can replace the part to be ignored with "_"

let (a, _, _) = stu


Welcome the guidance of various gods to learn together.

[Swift first look] Swift variables and constants

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.