Constants and variables for 3.Swift

Source: Internet
Author: User

1. Definition
    • letDefine constants that are not allowed to be modified once they are assigned, more secure
    • varDefine variables that can still be modified after assignment
    • You should select constants as much as possible before you need to modify them tovar
    • In Xcode 7.0, if the variable is not modified, Xcode will prompt to change tolet
Code Demo
ImportUIKit

ClassViewcontroller:Uiviewcontroller{

OverrideFuncViewdidload () {
Super.Viewdidload()
Demo()
}
FuncDemo () {
//Define a constant and assign a value directly
LetA =10

//Constants have been assigned values,Cannot be modified,Otherwise it will be an error
A =20

//Define the constant type in the assignment first
//Yellow warningSwiftThe new features,You are prompted to use this constant,If not applicable,Recommend that you use"_"To replace
LetB:Int
b =20

//Defining variables
Varc =20
        // variable definition can still be modified Value
        C = 30

   } }
2. Data typeThe default type of integer default type is int long decimal is double cgfloat single precisionshortcut key: Option+caps Lock View Interpretation3. Automatic derivationThe type of data in Swift is deduced automatically, depending on = right to determine the specific type of data
    • Swift can deduce the exact type of the variable according to the code on the right.
    • Typically at development time, you do not need to specify the type of the variable
    • If you want to specify a variable, you can use it after the variable name:, and then follow the class of the variable
4. Type conversionSwift is a very strict type of validation language different types of data cannot be calculated directlyimplicit type conversions are not supportedto perform a calculation, you must make a forced type conversion 
//Forcing type conversions
Letx2 =100
let y2 = 10.5
       
        let double (x2) + y2 Let num2 = x2 + Int(y2)
 5. Output function PrintFormat: Print (object)6. Specification writing code: Operators need to add spaces apart

Constants and variables for 3.Swift

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.