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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.