Knowledgebase

Swift Type System Fundamentals Print

  • appletechnology, apple, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Values, references and safety.

WHAT VALUE TYPES ARE

Structures and enumerations, copied when assigned or passed.

WHAT REFERENCE TYPES ARE

Classes, shared when assigned or passed.

WHY THAT DISTINCTION MATTERS MOST

It determines whether a change in one place is visible in another.

WHAT THE LANGUAGE PREFERS

Value types, by default.

WHY

Copies cannot be changed unexpectedly by other code, which eliminates a class of bug.

WHAT COPY-ON-WRITE PROVIDES

Values behaving as copies while sharing storage until one is modified.

WHY THAT MATTERS

Copying large collections is cheap until something changes.

WHEN TO USE A CLASS

When identity matters, or when the object must be shared deliberately.

WHAT AN ENUMERATION WITH ASSOCIATED VALUES PROVIDES

A type representing one of several cases, each carrying different data.

WHY THAT IS POWERFUL

It models state precisely, and the compiler requires every case to be handled.

WHAT THAT PREVENTS

States that should be impossible being representable.

WHAT TO AIM FOR

Making invalid states impossible to express.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot