Swift-definition and use of variables

Source: Internet
Author: User

To make use of. Let to declare constants, so that var to declare the variable.

? A constant value is not required at compile time, but you can only assign a value to it. So you can? Use constants to table? A value: You just have to decide, but you need to make it a lot of times.

       NSLog ("--------------")        var myvariable =        myvariable =        Mycontant =        lable = "The Is"

The types of constants or variables must be the same as the values you assign to them.

However, when declaring a type is optional, when the declaration is assigned at the same time, the compiler will determine the type of its own initiative.

In the example of the upper plane, the compiler determines that myvariable is an integerbecause its initial value is an integer.

Assuming the initial value is not provided? enough information (or no initial value), then you need to declare the type after the variable,? Cut with a colon.

Values are never implicitly converted to other types. If you need to convert a value to another type, explicitly convert it.

        Let width = 94;        Let widthlable = lable + (string) (width)        NSLog (String (), widthlable)

Is there a simpler way to convert a value to a string? Method: Write the value in parentheses and write it before the parentheses. a backslash.

Like what:

Let apples =3

Let oranges = 5

Let applesummary = "I hava\ (apples)apples"

Let fruitsummary = "I hava\ (apples+oranges)pieces of fruit"

To create an empty array or dictionary, use the Initialize syntax.

Let Emptarray = [String] ()

Let emptdictionary = Dictionary<String,Float> ()



Swift-definition and use of 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.