Type Property and Method

Introduction

Welcome to Lesson 7 of Object Oriented Swift. You are going to learn one of the most useful, yet confusing topics.

Problem

I’m confused by static, final, class. What are those?

Static Property and Method

No longer have to initialize an object to access properties and methods. Let us design a struct that contains a type property and a type method.

Design Struct

  1. struct SomeStructure {
  2. static var storedTypeProperty = "Some value."
  3. static var computedProperty: Int {
  4. get {
  5. return 120
  6. }
  7. set {
  8. print("You've set to \(newValue)")
  9. }
  10. }
  11. static func hello() {
  12. print("hello")
  13. }
  14. }

Access the properties and methods

  1. SomeStructure.storedTypeProperty // "Some Value"
  2. SomeStructure.hello() // 'hello"'

Anything that has static in front will not be overridden.

Class

Unlike static, class is only used within Classes. On the other hand, classes also support static.

Unlike static, class properties may be overridden.

Design Class

Create a class called GrandParent.

  1. class GrandParent {
  2. static var numberOfYearsInMarriage = 30
  3. static func introduce() {
  4. print("We've been married for \(numberOfYearsInMarriage)")
  5. }
  6. class func introducing() {
  7. print("We've been married for \(numberOfYearsInMarriage)")
  8. }
  9. final func cantOverride() {
  10. print("you can't change me")
  11. }
  12. }

Design Subclass

Now, create Parent class that inherits GrandParent. You may override class func introducing(), but you may not override final func cantOverride(). final prevents Classes from overriding.

  1. class Parent: GrandParent {
  2. override class func introducing() {
  3. print("I'm married for 5 years")
  4. }
  5. // override static func introduce() {
  6. // print("I'm married for 5 years")
  7. // }
  8. }
  9. Parent.introducing() // "I'm married for 5 years"

Practical Usage

You may store a list of items.

  1. struct BluetoothID {
  2. struct iPhone {
  3. static let iPhone4 = "4234-1232-1232-5465"
  4. static let iPhone5 = "7867-5676-4535-1235"
  5. static let iPhone6 = "3938-6738-1424-9876"
  6. static let iPhone7 = "4845-3148-1237-1296"
  7. static let iPhone8 = "7967-8123-7892-4563"
  8. }
  9. }
  1. BluetoothID.iPhone.iPhone8 // "4234-1232-1232-5465"
  2. BluetoothID.iPhone.iPhone8 // ""7967-8123-7892-4563"

Alternative

You may also use enum.

  1. enum AirDropID: String {
  2. case iPhone4 = "4234-1232-1232-5465"
  3. case iPhone5 = "7867-5676-4535-1235"
  4. case iPhone6 = "3938-6738-1424-9876"
  5. case iPhone7 = "4845-3148-1237-1296"
  6. case iPhone8 = "7967-8123-7892-4563"
  7. }
  8. AirDropID.iPhone8.rawValue // "7967-8123-7892-4563"

Source Code

2007_type_property.playground

Conclusion

You’ve learned how to access properties and methods without creating an object. When you work structs, you may only have static type. When you work classes, however, you may have static and class type. If you wish to prevent any property from being overridden, you may use the final keyword.

When you start taking The UIKit Fundamentals with Bob, you will discover the beauty of static methods and properties.

In the following lesson, you will learn how to create the only object that exists everywhere.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.