Swift Learning Day Two: Basic data types in Swift

Source: Internet
Author: User

One: Swift basic data type

    • Data types in Swift also include: integer/Float/object type/struct type, etc.
    • Understanding integer and floating-point types first
    • Integral type
      • have symbols
        • Int8: Signed 8-bit integer
        • Int16: Signed 16-bit integer
        • Int32: Signed 32-bit integer
        • Int64: Signed 64-bit integer
        • Int: Peace table related (default, Nsinteger equivalent to OC)
      • No sign
        • UInt8: Unsigned 8-bit integer
        • UInt16: Unsigned 16-bit integer
        • UInt32: Unsigned 32-bit integer
        • UInt64: Unsigned 64-bit integer
        • UINT: The Peace Table (commonly used, equivalent to OC Nsuinteger) (default)
    • Floating point Type
      • float:32 bit floating point type
      • double:64 floating-point type (default)
 //defines a variable m of type int, and is assigned 10var M: int = 10//defines a double type of constant N, and assigned a value of 3.14let N: double = 3.14 (Same as OC Swift Default floating-point data type is           Double    



II: Swift type deduction

    • Swift is a strong type of language
    • Any identifier in Swift has a definite type
    • Attention:
      • If an identifier is defined with a direct assignment, the type following the identifier can be omitted.
      • Because Swift has a type deduction, it automatically determines the data type of the preceding identifier based on the subsequent assignment
      • You can option 鼠标左键 see the data type of the variable by +
// 定义变量时没有指定明确的类型,但是因为赋值给i一个20.20为整型.因此i为整型var i = 20// 错误写法:如果之后赋值给i一个浮点型数值,则会报错// i = 30.5// 正确写法var j = 3.33j = 6.66


Three: operations of basic data types in Swift

    • In swift, you must ensure that the type is the same when you perform the basic operation, or you will get an error
      • Operations can be performed between the same types
      • Because there is no implicit conversion in swift
    • Conversion of data types
      • int type to double type: double (identifier)
      • The double type is converted to int type: int (identifier), instead of rounding, leave the decimal point behind and add 0.5 if you want to round it.
let a = 10let b = 3.14// 错误写法// let c = a + b// let c = a * b// 正确写法let c = Double(a) + blet d = a + Int(b)






Swift Learning Day Two: Basic data types in 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.