1. Constants & Variables

Source: Internet
Author: User

Simple experience
// 定义变量var i = 10println(i)i = 15println(i)let j = 20// 常量一经定义不能自改数值// j = 25println(j)
    • Periodic summary
      • varDefine variables that can be modified after they are set
      • letDefines constants that cannot be modified after they are set
      • Do not use at end of statement;
      • In Swift, use println() an alternative in OCNSLog
      • printlnPerformance is better and will be demonstrated later
Defined OCObject
 //materialized view let v = uiview (frame: cgrectmake (0, 0, 100, 100)) //set background color V.backgroundcolor = uicolor.redcolor () //added to root view View.addsubview (v)           
    • Periodic summary
      • In the Swift format that you want to instantiate an object 类名() with, the OC equivalent in alloc/init
      • OCinitWithXXX Swift can be used to 类名(XXX: ) find the corresponding function in the
      • OCA [UIColor redColor] class method in which Swift you can usually 类名.XXX find the corresponding function in the
      • Use let adornments v and assign values to indicate该常量的内存地址不允许修改,但是可以修改其内部的属性
      • The properties of the current object, which do not need to useself.

Constants & Variables Use principle: Try to use let first, only need to change, then use Var, can be more secure

Variable type
 let x = 10 let y = 10.5let z: double = 20println ( Double (x) + y) println (x + int (y))  println (y + z)              
    • Periodic summary
      • In the first contact, the Swift var let Swift type is very loose because of a simple mistake.
      • In fact, the exact type of all variables is automatically deduced at the same time as the assigned value.
      • Swiftis a very strict type of language,一个值永远不会被自动转换成其他类型
      • If you want to convert, you must display the conversion in Swift
        • Decimal default is Double type
        • Integer default is Int type
      • If you want to explicitly specify the type of a variable, you can define it by using thevar 变量名: 类型 = 值

1. Constants & 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.