Do I Use a Struct or a Class?

Whether you need to use a struct or class depends on a few factors, summarized here:

Image

You can see that you will mostly be using classes for your data structures. Here are a couple examples of good uses of structs:

■ A geometric point that contains an x and y and maybe a length

■ A geolocation that defines latitude and longitude

■ A geometric shape (like CGRect) that will contain width and height.

All these structs are simple and contain only a few simple data values (you are not limited to geometric data). They often represent single values like a point or a rectangle.

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.