Structures: similar to classes but more lightweight


struct theName {
  // the definition

Structures contain many of the same features as classes including: properties, methods, subscripts, initializers, and conforming to protocols. One feature that structures have over classes is an automatically generated memberwise initializer. This allows the values of the properties to be set by name. Technically structures, which in Swift include Strings, Arrays, and Dictionaries, are passed by value - so their data is copied every time a new reference is made. However, the Swift compiler optimizes for this case, so the end result is that there's virtually no overhead between passing by value or by reference.

It makes sense to use a Structure rather than a Class if the primary purpose is to group together a few simple data values, or if no inheritance is needed from another type. In general though, most custom data types should be classes rather than structures.

struct someStructure {
  var color = "blue"
  var scaleFactor = 1.0

var box = someStructure(color: red, scaleFactor: 2.0)
println("The scale factor for the box is \(box.scaleFactor)")
// prints The scale factor for the box is 2.0

In the above example, a structure is declared with two properties. An instance of the structure is created by using the memberwise initializer. The last line demonstrates the fact that the properties can be referenced by dot syntax.