Swift notes < 9 > enumeration and structure

Keywords: Swift

enumeration

Enumeration concept
  • Enumeration defines a set of related values of a common type that you can use in your code in a safe way
  • Enumeration in C/OC language specifies that the related name is a set of integer values
  • The enumeration in Swift is more flexible. It can provide a string, a character or an integer or floating-point value
Syntax for enumeration types
  • format
enum Direction {
   case east
   case west
   case south
   case north
}
Direction.east
//perhaps
enum Direction {
   case east, west, south, north
}
  • Standard
    • Enumeration type first letter lowercase
Assign to enumeration type
  • In swift, enumeration type does not represent any type by default, which is an identity
  • Enumeration type can be string / character / integer / floating point
     enum Direction: Int {
          case east = 2
          case west =3
          case south =4
          case north =5
     }
     //rawValue represents the original value corresponding to an enumeration
     //Enumeration value → original value
     let rv = Direction.east.rawValue
     //Original value → enumeration value
     let z Direction(rawValue : 6)
    
     //Enumerate binding strings
     enmu Path:String {
       case cache = "user/desktop/cache"
       case document = "user/desktop/doc"
     }
     func text (path:Path){
       if path == .cache {
                print(path.rawValue)
       }
      }
    

structural morphology

Basic use of structure
  • Concept of structure
    • A struct is a data set composed of a series of data of the same type or different types
    • Structure refers to a data structure
    • A struct is a value type, which is passed in a method
  • Structure format
    • struct structure name {property and method}
    • Both enumerations and structs can write methods
      • Type method: static func
      • Instance method: func
     struct Point {
        //Instance properties
        var x : Double
        var y : Double
        //Instance method
         func distance() {
             print("Calculate the distance between two points",x,Point.z)
     }
       //Type properties
        static var z :Double = 0
        //Type method
         static func dis() {
             print("xxx",z)
     }
     }
     Point.dis()
    
  • Calculate the distance between two points
     func distance(p1:Point ,p2:Point)→Double{
      return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2))
     }
    
Extended constructors for structs
  • By default, the structure automatically creates a "one by one constructor" → the purpose is to make all "non optional" member variables have values
  • Extension constructor
struct Point{
  //Non optional, never for nil
  var x :Double
  var y :Double
  var z :Double?
  //Custom constructor! = normal function
  //Without func
  //init must be used as the name
  //Inside a constructor, you must ensure that all non optional properties have values
  //If we define our own constructors now, there will be no one by one constructors automatically generated by the system
  init(x:Double,y:Double){
      self.x = x
      self.y = y
  }
  init(x:Double,y:Double,z:Double){
      self.x = x
      self.y = y
      self.z = z
  }
  
}
//When we use it directly, the "constructor" provided by the system = = "construct instance function"
//let p = Point(x:1,y:2)
//p.z = nil

let p = Point(x: 1, y: 2)
let p1 = Point(x: 1, y: 2, z: 3)

// Constructor generated by the system by default → one by one constructor
// Assign values to all the non optional attributes one by one. The purpose is to ensure that when an instance is created, all the non optional attributes have values

Posted by BinaryBird on Thu, 30 Apr 2020 13:44:31 -0700