Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning Swift

You're reading from   Beginning Swift Master the fundamentals of programming in Swift 4

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781789534313
Length 202 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Rob Kerr Rob Kerr
Author Profile Icon Rob Kerr
Rob Kerr
Kåre Morstøl Kåre Morstøl
Author Profile Icon Kåre Morstøl
Kåre Morstøl
Arrow right icon
View More author details
Toc

Arrays


An array is an ordered collection of elements of the same type, and they are used for pretty much anything that requires storing things in a certain order, such as the contents of lists in apps. It works like similar types in other languages.

Working with Arrays

Here are some of the most common operations on arrays:

  • Create an array:

    let a = [0,1,2,3,4] // array literal
  • Join two arrays:

    var b = a + [5,6]
  • Repeat a value:

    let c = Array(repeating: 4.1, count: 3)
  • Create an array from any sequence (a String is a Sequence of Character):

    var d = Array("The ☀ and 🌙 ")
  • Add values to the end of an array:

    b.append(10) // append one element
  • Append an entire array:

    b += a
    b.append(contentsOf: a) // append a sequence
  • Get the length of an array:

    b.count
  • Iterate over all elements:

    for nr in b {
             // do something with 'nr'
           }

Here are their abilities, represented by some of the protocols they conform to:

  • BidirectionalCollection can go backwards from any index (except...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image