Arrays are also structs, which means they too have their own methods and properties we can use to query and manipulate the array.
Here’s a simple array to get us started:
var toys = ["Woody"]
You can read the number of items in an array using its
If you want to add a new item, use the
append() method like this:
You can locate any item inside an array using its
firstIndex() method, like this:
That will return 1 because arrays count from 0.
Just like with strings, you can have Swift sort the items of the array alphabetically:
Finally, if you want to remove an item, use the
remove() method like this:
Arrays have lots more properties and methods – try typing
toys. to bring up Xcode’s code completion options.
SPONSORED Ever ask for help and your reviewer immediately notices issues you missed? Fernando Olivares is a 10-year veteran from Big Nerd Ranch and Lambda School who can help hone your app building skills, and he's just launched a new book that does just that – use the code "hacking" to save $5!
Link copied to your pasteboard.