NEW! Master Swift design patterns with my latest book! >>


Found 11 articles in the Swift Knowledge Base for this category:


How do you create multi-dimensional arrays?

Creating an array that holds strings is as simple as writing [String], but what about creating an array where each item is an array of strings – an array of arrays? This is called a multi-dimensional array, because if you were to draw its contents on paper it would look like a grid rather than a line.... Continue Reading >>

How to count objects in a set using NSCountedSet

One of my favorite little gems in Cocoa Touch is called NSCountedSet, and it's the most efficient way to count objects in a set. Here's the code:... Continue Reading >>

How to enumerate items in an array

There several ways to loop through an array in Swift, but using the enumerated() method is one of my favorites because it iterates over each of the items while also telling you the items's position in the array.... Continue Reading >>

How to find an item in an array using index(of:)

The index(of:) method tells you the index of an element in an array if it exists, or returns nil otherwise. Because it's an optional value, you need to unwrap it carefully or at least check the result, like this:... Continue Reading >>

How to join an array of strings into a single string

If you have an array of strings and want to merge all the elements together into a single string, it's just one line of code in Swift.... Continue Reading >>

How to loop through an array in reverse

If you want to read through an array in reverse, you should use the reversed() method. You can use this as part of the regular fast enumeration technique if you want, which would give you code like this:... Continue Reading >>

How to loop through items in an array

Swift offers a selection of ways to loop through an array, but the easiest and fastest is known as fast enumeration and looks like this:... Continue Reading >>

How to shuffle an array in iOS 8 and below

Nate Cook wrote a simple shuffle() extension to arrays that implements the Fisher-Yates shuffle algorithm in Swift. I use it a lot, or at least did until GameplayKit came along in iOS 9.0 – it has its own shuffle algorithm, and so is preferable.... Continue Reading >>

How to shuffle an array using GameplayKit

iOS 9.0 has a built-in way to shuffle arrays thanks to GameplayKit, and it's a simple one-liner. Here's an example of creating an array of lottery balls and picking six random ones:... Continue Reading >>

How to sort an array using sort()

All arrays have built-in sort() and sorted() methods that can be used to sort the array, but they are subtly different.... Continue Reading >>

How to tell if an array contains an object

It's easy to find out whether an array contains a specific value, because Swift has a contains() method that returns true or false depending on whether that item is found. For example:... Continue Reading >>

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions, all written for Swift 4.

Upgrade to the premium experience

Get all 40 projects in PDF and ePub, plus exclusive content that will take your Swift learning to the next level – buy the Hacking with Swift book today!

Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>