Access and modification of Swift array

Source: Internet
Author: User

1 Access and modification of an array can be done through the methods and properties of the arrays, or by using the array's next-label method. 2 3 to know the number of elements in an array, you can view its read-only property count:4println"The shopping list contains \ (shoppinglist.count) items.")5 //output "The shopping list contains 2 items."6 using the Boolean IsEmpty property, you can quickly check if the Count property is 0:7 ifShoppinglist.isempty {8println"The shopping list is empty.")9}Else {Tenprintln"The shopping list is not empty.") One}//output "The shopping list is not empty." A to add an element to the end of the array, you can call the Append method of the array: -Shoppinglist.append ("Flour") - //shoppinglist now contains 3 elements, and it looks like someone's going to have a pancake. theAdd an element to the end of the array, or you can use the + =Operator: -Shoppinglist + ="Baking Powder" - //Shoppinglist now consists of 4 elements. -You can also use + =operator to concatenate an array of the same type to the following array: +Shoppinglist + = ["Chocolate Spread","Cheese","Butter"] - //Shoppinglist now consists of 7 elements. + to get a value from an array, you can use the next banner method. In the parentheses immediately following the array name, pass in the index value of the element to be obtained: Avar FirstItem = shoppinglist[0] at //firstitem equals "Eggs" - Note that the index value of the first element of the array is 0, not 1. The swift array is always indexed starting at 0.  -  - you can use the subscript syntax to change an existing value for a given index: -shoppinglist[0] ="Six Eggs" - //The first item in this list is now "Six eggs", not "eggs". inYou can use the following banner to change the value of a specified range at once, even if the number of elements to be replaced is not the same as the number of elements that will be replaced. The following example will"Chocolate Spread","Cheese"And"Butter"Replaced by"Bananas"And"Apples" -shoppinglist[4...6] = ["Bananas","Apples"]//Shoppinglist now consists of 6 elements toNote: You cannot add a new element to the end of the array by using the following banner. Attempting to fetch or hold an element using a subscript that exceeds the range of the array results in a run-time error. Before using an index value, it should be compared to the array's Count property to detect if it is valid. Unless count is 0 (meaning this is an empty array), the maximum valid index of the array is always count-1, because the index of the array starts at 0.  + to insert an element into a specific location, you can call the array's insert (Atindex:) Method: -Shoppinglist.insert ("Maple Syrup", Atindex:0) the //Shoppinglist now consists of 7 elements * //The first element of the list is now "Maple syrup" $This call to the Insert function, through the specified subscript 0, adds a value to the beginning of the shopping list"Maple Syrup"the new element. Panax Notoginseng  - Similarly, you can use the Removeatindex method to remove an element from an array. This method deletes the element on the specified index and returns the deleted element (if you do not need a return value, you can ignore it): theLet Maplesyrup = Shoppinglist.removeatindex (0) + //An element with index 0 has been deleted from the array. A //shoppinglist now contains 6 elements and does not contain "Maple syrup" the //constant Maplesyrup is now equal to the deleted string "Maple syrup" +When an element is deleted, there is no space left in the array where there are no elements. So the element at index 0 becomes"Six Eggs"the following: -FirstItem = shoppinglist[0] $ //firstitem now equals "Six eggs." $ If you want to delete the last element of an array, you can use the Removelast method instead of using the Removeatindex method, which avoids having to call the array's Count property. Similar to the Removeatindex method, the Removelast method also returns the element that was deleted: -Let apples = shoppinglist.removelast ()//the last element of the array was deleted.//shoppinglist now contains 5 elements and does not contain "cheese"//constant Apples is now equal to the deleted string "Apples"

Access and modification of Swift array

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.