Step into Swift logo

Should I use a struct or a class?

Struct or Class

Struct vs Class

We use a struct when:

  1. Primary purpose is to encapsulate a few relatively small data values.
  2. Reasonably expect encapsulated values to be copied rather than referenced when assigned or passed around.
  3. Any properties stored by a struct are themselves value types which should also be copied.
  4. The struct does not need to inherit properties or behaviours from another type.

If your purpose does not fit into the above, use a class.

Related articles:

< All Posts