Swift Basic Learning (four) simple values

Source: Internet
Author: User

Use let to declare constants, and use Var to declare variables.

The compiler automatically infers the type without having to explicitly declare the type and assign the value at the same time as the declaration. If the initial value does not provide enough information (or no initial value), it is necessary to declare its type after the variable name, separated by a colon.

var myvariable = 42

myvariable = 50

Let myconstant = 42

Let Implicitinteger = 70

Let implicitdouble = 70.0

Let explicitdouble:double = 70.0//explicit specified constant type

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

Let label = "the width is"

Let width = 94

Let Widthlabel = label + String (width)

There is a simpler way to convert a value to a string: write the value in parentheses, and write a backslash in front of the parentheses.

Let apples = 3

Let oranges = 4

Let applesummary = "I has \ (apples) apples."

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

Use square brackets [] to create arrays and dictionaries, and use subscripts or keys to access elements. A comma is allowed after the last element.

var shoppinglist = ["Catfish", "water", "tulips", "Blue paint"]

SHOPPINGLIST[1] = "Bottle of Water"

var occupations = [

"Malcolm": "Captain",

"Keylee": "Machanic",

]

occupations["Jayne"] = "Public Relations"

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

Let Emptyarray = [String] ()

Let emptydictionary = [String:float] ()

If the type information can be inferred, you can use [] and [:] To create an empty array and an empty dictionary-just as you would declare a variable or pass arguments to a function.

Shoppinglist = []

Occupations = [:]

END

Swift Basic Learning (four) simple values

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.