Swift Learning-data types

Source: Internet
Author: User

Knowledge Points:

    1. specifying the data type of a constant \ variable
    2. Swift Strict requirements variables must be initialized before they are used
    3. Maximum : You can use the Min and Max methods to get the most value
    4. Range: Values exceeding storage range Swift will error
    5. Two variables of different types cannot be added
    6. number format: You can add 0 (0) or underline (_) to enhance readability
    7. Type Conversions
    8. Use the Typealias keyword to define the alias of type in swift, similar to the typedef in C
Example code:
: Playground-noun:a place where people can playimport uikit//Swift common data types are://Int, Float, Double, Bool, Character, S tring//Array, Dictionary, tuple type (tuple), optional type (Optional)//and the first letter of all data types is uppercase//1, specified variable \ constant data type///after constant \ variable name plus colon (:) and type name let Age:int = 10//Defines an INT type constant age, with an initial value of 10//it is generally not necessary to specify the type of variable \ Constant, similar to the type mechanism of JavaScript//2, Swift strict requirements variables must be initialized before use//3, Max let MI Nvalue = Uint8.minlet MaxValue = uint8.max//4, Range//In Swift, if the value exceeds the storage range, the compiler will directly error//But in objective-c, the compiler is only warning, recommended instead of long/ /Let Num:uint8 = 2143243253242343243253242 This sentence is error//5, type is different two variables \ Constants cannot be added let Fnum:float = 3.14//Float type let Dnum = 3.2  2//double type//The following sentence is error, two types are different cannot add/let result = Fnum + dnum//need to cast to result = Double (fnum) + dnum//6, number format// Additional 0 (0) Let money = 001999let Money2 = 001999.000//can be added with additional underscore _ to enhance readability let OneMillion1 = 1_000_000let overonemillion = 1_ 000_000.000_001//7, type conversion let NUM1 = 3let num2 = 0.14//the types of NUM1 and num2 have been determined here, so the following must be type-cast make sum = Double (NUM1) + num2//but under The face is correct let sum2 = 3 + 0.14//since March 0.14 added after determining the type of sum2, so is correct//8, Swift uses the Typealias keyword to define the type of alias, similar to the role of typedef in C Typealias MyInt = Intlet Mynum:myint = 20let Minmynum = Myint.min

Swift Learning-data type

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.