An array of Swift learning

Source: Internet
Author: User

First, the definition of the array: storing values of the same type in an orderly manner

(1) shorthand (shorthand) syntax for arrays

You can pass Array<element>, here, element, the type of the value of the array storage element. Can also be written in brackets [Element]

(2) Create an empty array

var emptyarr = [int] () (Here an empty array of type INT is created using the initialization method)
Emptyarr.append (3)
Emptyarr = [] (here is a literal statement to create an empty array)
Note:emptyarr is now a empty array,but is still an type of [Int]

In addition, I directly with var Emptyarr = [] Create an empty array will produce an error, reported that this is an immutable group, you can try

(3) Create an array with default values

Swift provides a way of initializing an array. And this array has the same default value

var Defaultarr = [double] (Count:3, repeatedvalue:0.0) (value type is Double type)
println (Defaultarr)

The println is [0.0, 0.0, 0.0]

(4) Create a new array from the add two array

Create a new array of two existing arrays of the same type by using the (+) number operation

var Anotherarr = [Double] (Count:3, repeatedvalue:2.5)
var newArr = Defaultarr + Anotherarr
println (NEWARR)

The result is [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

(5) Create an array with literal amounts

Enclose a series of values in brackets, separated by commas

var shopping:[string] = ["Eggs", "Milk"]

This declares an array that only stores character types, which are judged by the type of swift, and we are able to write the same type of values without having to specify the type to initialize the array with literal syntax, so the above example can be simpler.

var Shopping: = ["Eggs", "Milk"]

(6) Access and change arrays

We've learned to create an array, so we're going to go through an array and how to change an array?

To access and change arrays, we can use methods and properties, or under the banner method

We are able to know the number of elements of an array by simply reading the property (. count) of the array

println ("The shopping list contains \ (shoppinglist.count) Items") (The shopping list contains 2 items)

We are able to check the number of arrays and 0 for equality through the Boolen property (. isEmpty) of the array

If shoppinglist.isempty{
println ("The shopping list is empty")
}else{
println ("The shopping list isn ' t empty")
}

We are able to add elements to the last face of the array through the array ' s append (_:) method

Shoppinglist.append ("flour")
println (Shoppinglist) ([Eggs, Milk, flour])

A different approach. We can also add one or more elements via the (+ =) operation

Shoppinglist + = ["Baking Powder"]
Shoppinglist + = ["Chocolate Spread", "Cheese", "Butter"]
println (Shoppinglist) ([Eggs, Milk, flour, baking Powder, chocolate Spread, Cheese, Butter])

The ability to retrieve the value of an array by following the banner, followed by the array name with brackets. Write the subscript in which you want the value to be able to remove the corresponding subscript value, like OC. The first element is also calculated from the beginning of the 0

var FirstItem = shoppinglist[0]
println (FirstItem) (FirstItem is equal to "Eggs")

You can also change the value at the corresponding index by following the banner.

Shoppinglist[0] = "Six Eggs"
println (Shoppinglist) ([Six Eggs, Milk, flour, baking Powder, chocolate Spread, Cheese, Butter]) and the above control did change the first element

In Swift, you can change a range of values by following the banner, even if the length of the substituted value differs from the length of the range you substituted, such as

SHOPPINGLIST[4...6] = ["Bananas", "Apples"]
println (Shoppinglist)

The result of printing today is [six Eggs, Milk, flour, baking Powder, Bananas, Apples] that is, the array has 6 elements

SHOPPINGLIST[4...6] = ["Bananas", "Apples", "Bananas", "Apples"]

Now there are 8 elements in the array.

But please note that you cannot add elements to an array by following the banner method

Inserting elements into an array we are able to call the insert (_:atindex:) method

Shoppinglist.insert ("Maple syrup", atindex:0)
println (Shoppinglist) ([Maple syrup, Six Eggs, Milk, flour, baking Powder, Bananas, Apples, Bananas, Apples])

Similarly, you can remove an element by calling the Removeatindex (_:) method. And be able to receive the removed element (if you don't need it). You don't have to accept it, just ignore it!)

var removeditem = shoppinglist.removeatindex (0)
println (Shoppinglist) ([Six Eggs, Milk, flour, baking Powder, Bananas, Apples, Bananas, Apples])

Call RemoveRange to remove an array within a certain range

var Removedrangeitem: () = Shoppinglist.removerange (6...7)
println (Shoppinglist) ([Six Eggs, Milk, flour, baking Powder, Bananas, Apples])

Call the Removelast () syntax to move the last element of the divisor group. Instead of by calling the Removeatindex (_:) method. To avoid traversing the array again

Shoppinglist.removelast ()
println (Shoppinglist)

Traversal of an array

We can iterate through the for-in loop

println (Shoppinglist)
For item in shoppinglist{
println (item)
(Six Eggs
Milk
Flour
Baking Powder
Bananas)
}

Assuming you need the subscript and the corresponding value for each element, you need to use the enumerate () method to iterate through the array, returning a tuple that contains the index of the element and the corresponding value

for (Index,value) in enumerate (shoppinglist) {
println ("item\ (index): \ (value)")
}

Item0:six Eggs
Item1:milk
Item2:flour
Item3:baking Powder
Item4:bananas
This is the swift2.0 before, 2.0 after the use of

for (Index,value) in Shoppinglist.enumerate () {
println ("item\ (index): \ (value)")
}

This method


Reference: Https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_ language/collectiontypes.html#//apple_ref/doc/uid/tp40014097-ch8-id105

An array of Swift learning

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.