Constants and variables

Source: Internet
Author: User

使用let来声明常量,使用var来声明变量

Variable
    • Oc
      • Define re-initialize firstint num; num = 10
      • Simultaneous initialization of the definitionint num = 20
    • Swift
      • Define re-initialize firstvar num
        • Error: No data type specified (type annotation missing in pattern), if you want to define a variable in swift, you must tell the compiler variable's type (type callout) when you use it later.
var num: Intnum  = 10
- 定义的同时初始化    - 在Swift中如果定义的同时初始化一个变量,可以不用写数据类型, 编译期会根据初始化的值自动推断出变量的类型(其它语言是没有类型推断的)    - 以后在开发中如果在定义的同时初始化就没有必要指定数据类型, 除非需要明确数据类型的长度或者定义时不初始化才需要指定数据类型
var num2: Int = 20var num3 = 20
You can use any character you like as a constant and variable name, including Unicode characters, constants and variable names that cannot contain mathematical symbols, arrows, reserved (or illegal) Unicode code points, lines and tabs. You cannot start with a number, but you can include a number elsewhere in a constant and variable name. ”
var ?? = 100var 哈哈 = 300
    • Error:
var 3x = 10var x+-3 = 10
Constant:
    • oc:const int num = 10;
    • Swift:let num = 10
    • Error:let num : Int
      • Constants in Swift must be initialized at the time of definition (OC can be uninitialized) or an error will be
    • Use of constants: Some values do not need to be changedlet num4 = 10

Constants and variables

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.