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 [Instructor Edition]
  • Table Of Contents Toc
Beginning Swift [Instructor Edition]

Beginning Swift [Instructor Edition]

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

Beginning Swift [Instructor Edition]

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

Function Type


Note

Open Functional.playground at the Introduction page.

First, let's reiterate what a function type is:

var sum: (Int, Int) -> Int

The type of sum is a function that takes two Int values and returns one Int value. We can assign both functions and closures to it, as they are essentially the same thing:

func sumFunction(a: Int, b: Int) -> Int {
  return a + b
}
let sumClosure = {(a: Int, b: Int) in return a + b}

sum = sumFunction
sum = sumClosure

We can also assign an operator to it:

sum = (+)

This is because an operator is a function (the parentheses around the + operator are just to signal that we want to use it as a function, not add things together right away). The definition of the + operator for Int is:

static func +(lhs: Int, rhs: Int) -> Int

So whenever a function has a parameter of a function type, we can supply an operator, as long as the input and output match:

func perform(operation: (Int, Int) -> Int...
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Beginning Swift [Instructor Edition]
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

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

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon