Skip to content
  • Services
  • About
  • Partners
  • Work
  • Insights
  • Careers
  • Contact
  • Services
  • About
  • Partners
  • Work
  • Insights
  • Careers
  • Contact
September 15, 2015

Classes and Structs in Swift

Web and Mobile Development

The differences between let and var, class and struct.

With the introduction of Swift, native iOS development has become more approachable and modern. Classes and structs in Swift are basic constructs of your program. In Swift, both classes and structs can have properties and functions. The key difference is structs are value types and classes are reference types. Because of this, let and var behave differently with structs and classes.

Mutability

Using let on a struct makes that object a constant. It cannot be changed or reassigned and neither can its variables. A struct created as a var can have its variables changed.

The struct milo was created with let therefore it cannot have its variables changed. The dog cannot be renamed.

The struct meg was created as a variable, therefore we can change its variables and rename the dog.

Since classes are reference objects the only difference between let and var is the ability to reassign the variable to a different class of the same type. The let and var keywords do not affect the ability to change a variable on a class.

The above example shows the difference between let and var with classes. Julia cannot upgrade her car since it’s immutable (let). She can however correct the model of the car she actually purchased. Shelli can upgrade to the R8 because her car is a var.

Mutating Functions

Functions marked as mutating change internal property values. Mutating functions are only present on structs and can only be used on structs created as a var.

The apple cannot be eaten since it’s immutable (let) unlike the orange which can be eaten because it’s a variable.

Modifying structs in external functions

Since structs are value types, they are pass by value. This means their contents will be copied when passed into functions. A struct variable can be marked as an inout parameter if it needs to be modified inside a function and its value persisted outside of the scope of the function.

Final Thoughts

In Swift structs and classes give you both value and reference-based constructs for your objects. Structs are preferred for objects designed for data storage like Array. Structs also help remove memory issues when passing objects in a multithreaded environment. Classes, unlike structs, support inheritance and are used more for containing logic like UIViewController. Most standard library data objects in Swift, like String, Array, Dictionary, Int, Float, Boolean, are all structs, therefore value objects. The mutability of var versus let is why in Swift there are no mutable and non-mutable versions of collections like Objective C’s NSArray and NSMutableArray.

Posted in Web and Mobile Development
Share this

Carlos Paelinck

Carlos Paelinck is a web and Apple platforms developer. He attends and speaks at the Angular and JavaScript meetups in Omaha, NE. Carlos enjoys his dog, gin cocktails, golf and Nebraska Cornhuskers football. During his time with Modus Create, Carlos filled the role of Software Engineer.
Follow

Related Posts

  • Swift
    Writing a Cordova Plugin in Swift 3 for iOS

    This blog post is an update to one I wrote back in April 2016 and…

  • Swift
    Writing a Cordova Plugin in Swift 3 for iOS

    This blog post is an update to one I wrote back in April 2016 and…

Subscribe to the Modus Newsletter

Receive the latest insights from our team each month.

Let's Chat

If forms aren’t your thing, you can always call us (+1-855-721-7223).

Modus-Logo-Primary-White.svg
  • Services
  • About
    • Newsroom
  • Partners
  • Work
  • Insights
    • Blog
    • Modus Labs
  • Careers
Virginia (US)

12100 Sunset Hills Road
Suite 150
Reston, Virginia, 20190
Tel: +1-855-721-7223

California (US)
12130 Millennium Dr

Los Angeles, CA 90094

Missouri (US)
609 E High St

Jefferson City, MO 65101

Romania

Str. Mihai Veliciu, no. 17
Cluj-Napoca, Romania
Tel: +40-0786-887-444

Costa Rica

2nd Floor, Plaza Koros, Av 3
San José, Santa Ana, Costa Rica

© 2021 Modus. All Rights Reserved.

Privacy Policy | Accessibility Statement | Sitemap

Scroll To Top
This website uses cookies.
These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience, and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.

Accept
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Save & Accept