Swift Learning notes-arrays

Source: Internet
Author: User

The array in Swift is denoted by []

1. Create an empty array

Let Emptyarr = [String] ()

Let emptyArr1: [NSNumber] = []

Both of these methods can be

2. Accessing elements

var numarr=["1", "2", "3"]

println ("first element: \ (Test1array[0])")

println ("last element: \ (test1array.last)")

3. Determine if the array is empty

Emptyarr.count ==0 or Emptyarr.isempty

4. Modifying an array

var strarr = ["A", "B", "C", "D", "E", "F", "G"]

strarr[1]= "X"

strarr[2...5]=["C", "D", "E", "F"]

Print (Strarr)

5. Array loops

for (INDEX,STR) in Strarr.enumerate () {

Print ("\ (index): \ (str)")

}

6. Array insertion

1 adding elements at the end

Strarr.append ("O")

2 Insert to specified position

Strarr.insert ("Y", atindex:1)

Print (Strarr)

7. Array Delete element

1 Delete the element at the specified position

Strarr.removeatindex (1)

2 Delete last Element

Strarr.removelast ()

Print (Strarr)

Note: Swift requires an array to be able to use two points correctly

1, the array must be initialized

2, you must specify the data type, you can assign a value to the compiler to automatically recognize the data type, or you can specify it yourself.

Swift Learning notes-arrays

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.