Methods in Classes

A method and a function are very similar except for a couple differences (aside from scope issues):

■ A method has a reference to the object that it belongs to.

■ A method is able to use data contained within the class it belongs to.

A method is identical to a function in syntax. Type methods in Swift are similar to Objective-C class methods. A big difference between from C and Objective-C to Swift is that in Swift you can define methods on classes, structs, and enums. This gives you great flexibility and strength. In Swift you have a couple different types of methods available to you. Let’s start with instance methods.

Instance Methods

Instance methods are likely the type of methods you will be creating most of the time. ...

Get Learning Swift™ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.