Swift array literal

Source: Internet
Author: User

1 You can initialize an array with an array literal, simply by putting one or more values together. The array literal is written as a comma-separated line of values, and the lines are wrapped up in parentheses with one end of each other:2[Value1, value2, value3]3 4 The following example creates an array that holds a string of type value named Shoppinglist:5var shoppinglist:string[] = ["Eggs","Milk"]6 //shoppinglist initialization with two elements7According to string[], the variable shoppinglist is declared as "an array that holds string values." Because this array indicates that the value type is string, it can only hold a value of type string. The shoppinglist array here is initialized with an array literal that contains two elements of type string ("Eggs"And"Milk"). 8 Note: The array shoppinglist is declared as a variable (with the keyword Var) instead of a constant (with the keyword let) because more elements are added to the shopping list in the following example. 9 in this case, the array literal contains only two string values. This is consistent with the type in the shoppinglist variable definition (an array that holds string values), so you can assign the array literal directly to the shoppinglist for initialization. Ten  One thanks to Swift's type inference, if you use array literals to initialize an array, the values in the array literal have the same type, and you don't have to explicitly write out the type of the array. The code for initializing shoppinglist above can be abbreviated as: Avar shoppinglist = ["Eggs","Milk"]  -Because all values in the array literal have the same type, swift can infer that the type of the shoppinglist variable is string[].

Swift array literal

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.