O'Reilly logo

Learning Swift™ Programming by Jacob Schatz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required