Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Beginning Swift
  • Table Of Contents Toc
  • Feedback & Rating feedback
Beginning Swift

Beginning Swift

By : Rob Kerr, Kåre Morstøl
4 (1)
close
close
Beginning Swift

Beginning Swift

4 (1)
By: Rob Kerr, Kåre Morstøl

Overview of this book

Take your first foray into programming for Apple devices with Swift.Swift is fundamentally different from Objective-C, as it is a protocol-oriented language. While you can still write normal object-oriented code in Swift, it requires a new way of thinking to take advantage of its powerful features and a solid understanding of the basics to become productive.
Table of Contents (10 chapters)
close
close

Functional Methods


Note

Open Functional.playground at the Methods page.

Note

The following sections show different ways of performing the same tasks. They say nothing about which version is better.

filter

The filter method looks like this:

func filter(_ isIncluded: (Element) throws -> Bool) rethrows -> [Element]

It is a simple method on Sequence, and we have already used it. The input function takes an element of the sequence and returns either false or true. filter returns an array of only those elements for which the input function returns true:

let numbers = [-4,4,2,-8,0]
let negative = numbers.filter {$0<0} // [-4, -8]

Set and Dictionary have their own versions of this method, which return a Set or Dictionary respectively.

Using the filter Method

It is often simpler to use an existing collection that has everything we want, and filter it, than creating an empty collection...

Unlock full access

Continue reading for free

A Packt free trial gives you instant online access to our library of over 7000 practical eBooks and videos, constantly updated with the latest in tech

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY