Swift Foundation 1.1--Basic syntax-variables and constants

Source: Internet
Author: User

Some days ago. After the first swift developers Conference was opened. A lot of friends around OC can not restrain.

Think it's time to learn swift, after all, Swift is a trend.

It was also their repeated request, let me tidy up the swift learning experience. Today, starting with Swift's basic grammar,


definition
    • letDefine constants, once assigned, do not agree to change
    • varDefines a variable. Can still be changed after assignment

//define constants and set values directly LetX:Int=Ten//constant value once set, can not be changed, the following code will error//x =//Use ': type '. Only the type is defined. Without setting a value LetY:Int//constants have an opportunity to set values once. There is no problem with the following code. Because ' Y ' has not been set to a valuey =Ten//Once the value has been set. You cannot change it again. The following code will error, because ' Y ' has been set the value//y =Print(x + y)//variable can continue to change value after setting the valuevarZ:Intz = -z = $Print(x + y + z)
self-motivated deduction
    • Swift can deduce the exact type of the variable according to the code on the right.
    • Usually at development time. You do not need to specify the type of the variable
    • Suppose you want to specify a variable. You can use it after the variable name : , and then follow the type of the variable

No implicit conversions
    • Swift is extremely strict with data type requirements
    • No matter what time. It's not going to be an implicit conversion.

Let & var options
    • The constants should be chosen as much as possible , only if they have to be changed tovar

Swift Foundation 1.1--Basic syntax-variables and constants

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.