Swift Chinese Tutorial (ii) Simple values

Source: Internet
Author: User
Tags constant empty

Swift uses the LET keyword to declare constants, and the Var keyword declares variables. Constants need not be specified at compile time, but must be assigned at least once. In other words, the assignment is used more than once:

1 var myvariable = 42
2 myvariable = 50
3 Let Myconstant = 42

The value cannot be changed after the constant assignment here, and should be more reusable.

The value of a constant or variable must be the same as the type. However, you do not need to specify its type, because the compiler infers its type based on the value you assign, and in the example above, the compiler will determine that myvariable is an integer (integer) because its initial value is an integer.

If the information for the initial value is not clear enough (so that the type is not judged), you can specify the type with a colon after the variable name:

1 Let Implicitinteger = 70
2 Let implicitdouble = 70.0
3 Let explicitdouble:double = 70

Practice:

Create a constant with a type of float and a value of 4.

Let Implicitfloat:float = 70

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Values will never be implicitly converted to other types. If you need to convert a value to a different type, explicitly construct an instance of the desired type.

Let label = ' the width is '
Let width = 94
Let Widthlabel = label + String (width)

Practice:

Try to delete the string method, what error would you get?

There is also a simpler way to have a value in a string: Place the value inside the parentheses and start with a backslash, such as:

Let apples = 3
Let oranges = 5
Let applesummary = "I have \ (apples) apples."
Let fruitsummary = "I have \ (apples + oranges) pieces of fruit."

Practice:

Use \ () to include a floating-point number to the string and include someone's name to greet.

Let pie:double = 3.14

Let pin:double = 3.15

Let Greetpie = "hello,\ (pie + pin)"

Create an array or dictionary with [] and access using a subscript or key name:

"var shoppinglist = [" Catfish "," water "," tulips "," Blue paint "]  
shoppinglist[1] =" Bottle of water "
       
var occupatio NS = [  
    "Malcolm": "Captain",  
    "Kaylee": "Mechanic",  
]  
occupations["Jayne"] = "public Relations"

Creates an empty array or dictionary, using an initialization assignment statement:

1 "Let Emptyarray = string[] ()
2 Let emptydictionary = Dictionary<string, float> () "

If the type information cannot be inferred, you can write an empty array--"[]" or an empty dictionary--"[:]", for example, if you assign a new value to a variable or a function to pass a parameter to:

Shoppinglist = []//went shopping and bought everything.

Author: cnblogs Joe.huang

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.