Classes And Structures In Swift

Introduction

 
Classes and structures are building blocks of program code. The properties and methods are defined to add functionality inside the class and structures. Swift does not require the creation of a separate interface. The instance of a class is called an object. 
 
Both can define properties to store values and define methods to provide functionality.
 
Both can define subscripts to provide access to their value using subscript syntax and define initializer to set up the initial state.
 
Syntax
 
Class and structures are similar in syntax and class are declared by class keyword and structure are declared by struct keyword. The class definition is defined within the pair of braces. 
  1. class ClassName      
  2. {      
  3.  // class definition      
  4. }      
  5.       
  6. struct StructName      
  7. {      
  8.  // struct defnition      
  9. }       

Class and Structure Instances

 
The structure and class do not describe a specific resolution or mode. The instance of structure or class is created to perform the operation. Structure and class use initializer syntax for a new instance. The simplest form of initializer syntax uses the type of the class or structure followed by empty parentheses. This creates a new instance of a class with any properties initiated to their default values.
  1. let someResolution = Resolution ()  
  2. let someVideoMode = VideoMode ()  
Accessing properties
 
The properties of instance is accessed by using dot syntax. The property name is written immediately after the instance name separated by period (.) without spaces. Shift enables to set sub-properties of structure property directly. The subproperties can be drilled down such as the width property in the resolution property of videoMode.
  1. println ("width of someResolution is \(someResolutio.width)")  
Output
 
The width of someResolution is 0.
 
Memberwise Initializer
 
All structures contain automatically generated memberwise initializer, and it is used to initialize the member properties of new structure instances. Initial values for properties of the new instance can be passed to memberwise initializer. Classes instance do not receive a default memberwise initializer.
  1. let vga = Resolution (width : 640, height : 480)  
Structure- value types
 
A value type is copied when it is assigned to a variable or constant, or when it is passed to a function. All structures are value types in swift. The properties of value types are always copied when they are passed in the code. When rememberedDirection is assigned the value of currentDirection, It is actually set to a copy of that value. Changing the value of currentDirection thereafter does not affect the copy of the original value that was stored in rememberedDirection.
  1. enum CompaPoint {  
  2.     case North, South, East, West  
  3. }  
  4. var currentDirection = CompassPoint.West  
  5. let rememberedDirection = currentDirection  
  6. currentDirection = .East  
  7. if rememberedDirection == .Wesr {  
  8.     println("The remembered direction is still .West")  
  9. }   
Output
 
The remembered direction is still .West 
 
Classes- Reference types
 
Reference types are not copied when they are assigned to a variable or ehen they are passed to a function. It is possible for multiple constants and variables to refer same single instance of clas behind the scenes. Use of the videoMode class is defined below:
  1. let tenEighty = VideoMode()  
  2. tenEighty.resolution = hd  
  3. tenEighty.interlanced = true  
  4. tenEighty.name = "1080i"  
  5. tenEighty.frameRate = 25.0   

Methods

 
Methods are functions that are associated with a particular type. Classes define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. classes define type methods that are associated with the type itself. 
 
Instance method
 
Instance methods are functions that belong to instances of a particular class. Instance method has the same syntax as function syntax and it supports the functionality of instances by providing ways to access and modify instance properties.
 
Counter class defines three instance methods: increments by counter 1, increments the counter by specified integer amount, resets the counter to zero.
  1. class Counter {  
  2.     var count = 0  
  3.     func increment() {  
  4.         count++  
  5.     }  
  6.     func incrementBy(amount: Int) {  
  7.         count += amount  
  8.     }  
  9.     func reset() {  
  10.         count = 0  
  11.     }   
Type Method
 
The type method for classes is indicated by writing the keyword class before the method func keyword and type methods for structures and enumerations by writing the keyword static before the method func keyword.
  1. class SomClass {  
  2.     class func someTypeMethod() {  
  3.         // type method definition    
  4.     }  
  5. }  
  6. }  


Similar Articles