Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between type method and type instance method, etc?

Note: I've read the apple documentation and studied a swift book.

  1. I'm confused on the difference between a "type instance method" (if such exists, correct me if I'm wrong) and a type method?

  2. Difference between class method and instance method?

  3. Difference between type property and instance property(if such exists, sorry I'm very confused on Type Properties subject)?

  4. Lastly, Do class properties exist in swift?

Sorry for the confusion :'(

like image 920
Ali Avatar asked Sep 14 '15 21:09

Ali


People also ask

What is difference between class method and instance method?

Key TakeawaysInstance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self .

Is type and method the same?

Now, a type method is a method that can be called directly on the type without creating an instance of that type. Notice that there is a static qualifier before the func . This indicates that it pertains to the type directly and not to an instance of the type.

What is the difference between instance method and class method in Java?

Instance methods can access class variables and class methods directly. Class methods can access class variables and class methods directly. Class methods cannot access instance variables or instance methods directly—they must use an object reference.

What are instances and methods?

An object that is created using a class is said to be an instance of that class. We will sometimes say that the object belongs to the class. The variables that the object contains are called instance variables. The methods (that is, subroutines) that the object contains are called instance methods.


2 Answers

In Swift, types are either named types or compound types. Named types include classes, structures, enumerations, and protocols. In addition to user-defined named types, Swift defines many named types such as arrays, dictionaries, and optional values. (Let's ignore compound types for now since it doesn't directly pertain to your question.)

To answer your questions, suppose that I create a user defined class called Circle (this is just an example):

class Circle {

    static let PI = 3.14

    var radius: Double

    init(radius: Double) {
        self.radius = radius
    }

    // Returns the area of this circle
    func area() {
        return PI * radius
    }

    // Ridiculous class method for demonstration purposes
    static func printTypeName() {
        println("Circle")
    }
} 
  1. I'm confused on the difference between a "type instance method" (if such exists, correct me if I'm wrong) and a type method?

As mentioned earlier, a type refers to a class, structure, enumeration, protocol, and compound types. In my example above, I use a class called Circle to define a type.

If I want to construct an individual object of the Circle class then I would be creating an instance. For example:

let myCircleInstance = Circle(radius: 4.5)
let anotherCircleInstance = Circle(radius: 23.1)

The above are objects or instances of Circle. Now I can call instance methods on them directly. The instance method defined in my class is area.

let areaOfMyCircleInstance = myCircleInstance.area()

Now, a type method is a method that can be called directly on the type without creating an instance of that type.

For example:

Circle.printTypeName()

Notice that there is a static qualifier before the func. This indicates that it pertains to the type directly and not to an instance of the type.

  1. Difference between class method and instance method?

See the explanation above.

  1. Difference between type property and instance property(if such exists, sorry I'm very confused on Type Properties subject)?

This is a similar explanation to the one in your question one except that instead of applying to methods, it applies to the properties (i.e., attributes, variables) of the type.

In my Circle example, the properties are defined as:

static let PI = 3.14
var radius: Double

The property PI is a type property; it may be accessed directly by the type

Circle.PI

The property radius is an instance property of the type; it may be accessed by an instance of the type. Using the variables we created earlier:

// I can do this; it will be 4.5
myCircleInstance.radius

// And this; it will be 23.1
anotherCircleInstance.radius

// But I CANNOT do this because radius is an instance property!
Circle.radius
  1. Lastly, Do class properties exist in swift?

Absolutely! Read my explanation to your question 3 above. The PI property in my example is an example of a class property.

References:

  • Swift Language Reference - Types
  • Swift Language Reference - Properties
  • Swift Language Reference - Methods
like image 116
whyceewhite Avatar answered Sep 23 '22 12:09

whyceewhite


The difference is that instance methods and properties are created for every instance. Type methods and properties are created for the whole type

Let's say you have struct Employee

struct Employee {
  static var ID:Int = 0
  static var NAME:Int = 1

  static func getNameOfField(index:Int) -> String {
      var fieldName:String = ""

      if index == ID {
          fieldName = "Id"
      } else if index == NAME {
          fieldName = "Name"
      }

      return fieldName
  }

  var fields = [Int:String]()

  mutating func config(id:String, name:String) {
      fields[Employee.ID] = id
      fields[Employee.NAME] = name
  }

  func getField(index:Int) -> String {
      return fields[index]!
  }
}

var e0 = Employee()
e0.config("1", name: "Mark")

var e1 = Employee()
e1.config("2", name: "John")

print(e0.getField(Employee.NAME))               // prints "Mark"
print(Employee.getNameOfField(Employee.ID))     // prints "Id"

Each instance of the struct e0 and e1 has property fields. It is created for every instance and lives in it. The values stored in the fields property are different for every instance. That's why it is called "instance property"

Each instance also has method getField. It is created for every instance and it has access to its properties and methods in that case to the instance's var fields. That's why it is called "instance method"

You access instance properties and methods by referencing the instance (in our case e0 and e1)

ID and NAME are type properties or static properties. The are created only once and have the same value for every instance and for every other object. You access type properties by referencing the "type" (in our case the struct) Employee.NAME

Type methods are something like a global function for a type(struct, class, enum). They are usually used to encapsulate functionality that is bound to the type but may not require an instance. Like in the example the type method getNameOfField(index:Int) -> String returns the field's name based on an index. To return this information you don't have to create na instance of Employee

Types are structs, classes and enums

You define type methods and properties with the keyword static (that's why they are also called static methods and properties)

Structs and enums can have type properties and type methods. Classes can only have type methods. You can create type property but it has to be computational

In classes you can define type methods with static or class keyword. The difference is that class methods can be overridden.

like image 36
Nikolay Nankov Avatar answered Sep 20 '22 12:09

Nikolay Nankov