O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

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

Stored properties

A stored property is a variable or constant that is stored as part of an instance of a class or structure. These are defined with the var and let keywords just like normal variables and constants. In the following code, we will create a structure named MyStruct and a class named MyClass. The structure and the class both contain two stored properties, c and v. The stored property c is a constant because it is defined with the let keyword, and v is a variable because it is defined with the var keyword. Let's look at the following code:

struct MyStruct {  
  let c = 5 
  var v = "" 
} 
 
class MyClass { 
  let c = 5 
  var v = "" 
} 

As we can see from the example, the syntax to define a stored property is the same for both classes and structures. ...

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