Classes and structures are the general purpose containers in Swift. Both have properties to store values, have methods to add functionality, can use initializers to set up initial state, can be extended to have more functionality, and can conform to protocols to standardize interaction. Classes have a few extra capabilities than structures, but both types of container are functionally very similar.
Swift doesn't require any extra interface or implementation files. You define your class or structure in one file and it is automatically made available throughout the rest of your code.
Creating properties for your class or structure is the same as declaring variables and constants. Declaring methods is as easy as declaring functions.
Creating a structure or class is as easy as using the class
or struct
keyword:
class Book { var title: String var author: String var currentOwner: String func getTitleAndAuthor() -> String { return title + author } init(withTitle title:String, andAuthor author:String) { self.title = title self.author = author self.currentOwner = "" } }
We've now declared a class of type Book
. Any instance of this class will have several properties for the title and the author and a method to return a formatted string.
Creating a new instance is as easy as calling the init
method:
var favouriteBook = Book(withTitle: "Neuromancer", andAuthor: "William Gibson")
You can access the properties with the dot syntax:
favouriteBook.author // William Gibson favouriteBook.title // Neuromancer
Calling methods is also very easy:
favouriteBook.getTitleAndAuthor() // NeuromancerWilliam Gibson
Both author
and title
are constants within the class, so they're read only. The currentOwner
property is a variable and is therefore writable. You can assign that value to our new instance:
favouriteBook.currentOwner = "brett ohland" favouriteBook.currentOwner // returns brett ohland
Even though classes and structures are similar in a lot of ways, there are fundamental differences and situations where one may be better to use than the other.
To talk about the differences, we need to talk about value types versus reference types.
We've seen value types all over our examples. Types such as Int
, String
, Bool
or Double
as well as collection types such as array and dictionary are all value types. This simply means that when you do this:
let newString = string
The newString
will be initialized with a copy of the original string. Modifying the original string won't modify the new string we just created.
Reference types, on the other hand, are not copied during assignment and instead are just given a reference to the original object. We created a Book
class earlier in this section and are using this for this example:
let favouriteBook = Book(withTitle: "Neuromancer", andAuthor: "William Gibson") let newBook = favouriteBook
The constant newBook
is simply a reference that points to favouriteBook
. If we were to change a property on newBook
it would be reflected in the original favouriteBook
object (or vice versa):
var favouriteBook = Book(withTitle: "Neuromancer", andAuthor: "William Gibson") var newBook = favouriteBook newBook.currentOwner = "brett ohland" favouriteBook.currentOwner // brett ohland newBook.currentOwner // brett Ohland
A structure is a value type and a class is a reference type. A structure is best used if your objects are relatively simple representations of data that don't need to inherit any other functionality and whose properties are also value types.
Classes, on the other hand, being reference types, have the ability to inherit other classes, be cast as a different type and have the ability to deinitialize themselves to free up resources.
You can use an extension
to add features and functionality to any existing class or structure. For example, let's extend the Int
type to have a method called square. This method will be able to be called on any instance of an Int
using dot notation:
extension Int { var square:Int { return self * self } }
Now getting the square root of any Int
is as easy as adding .square
to it:
4.square // 16 2.square // 4
Classes and structures are fully featured and complex concepts in Swift. Many of these advanced topics, such as protocols and generics, are outside the scope of this book and can be found at https://developer.apple.com/swift/resources/
3.129.70.157