0

Been searching SO and thru google for an hour. Haven't found an exact answer. Hence please verify my understanding.

Struct vs. Class

  1. Struct is by default preferred in swift.
  2. Struct is value type. Class is reference type

pic from : https://cocoacasts.com/value-types-and-reference-types-in-swift enter image description here

Okay. That's all fine and dandy. Now What's the difference between static func and func within a class?

static just means that -> Static, but when it's within a class and used to declare a func? What does it mean?

static keyword is same like final class. final keyword makes the variable or function final i.e. they can not be overridden by any inheriting class. (link)

class TestStruct {
  var count = Int()

  
  func popeye(name: String) -> String {
    count = count + 1
    return "TestStruct - func popeye - name:\(name) Count:\(count)"
  }
  
  static func brutus(name: String) -> String {
    var count = Int() // when declared within the static func
    count = count + 1 // this never gets incremented 

    return "TestStruct - static func brutus - name:\(name) count:\(count)"
  }
}

I tried this and found out that I can't do foo1.brutus as I can do foo1.popeye when it's assigned with a static func keyword.

But as just a func, I can have 2 variables referencing the same func and both will have it's own value (example below,count output is different). What's the benefit of using static then? When do I use static func

let foo1 = TestStruct()
let foo2 = TestStruct()
var bar1 = foo1.popeye(name: "popeye sailorman")
var bar2 = foo2.popeye(name: "popeye spinach  ")

print("foo1:\(bar1)")
print("foo2:\(bar2)")

bar1 = foo1.popeye(name: "popeye sailorman")
print("foo1:\(bar1)")
print("foo2:\(bar2)")

bar1 = foo1.popeye(name: "popeye sailorman")
print("foo1:\(bar1)")
print("foo2:\(bar2)")

bar1 = foo1.popeye(name: "popeye sailorman")
bar2 = foo2.popeye(name: "popeye spinach  ")
print("foo1:\(bar1)")
print("foo2:\(bar2)")


var oliveOil1 = TestStruct.brutus(name: "Brutus Big ")
var oliveOil2 = TestStruct.brutus(name: "Brutus Mean")
print("oliveOil1:\(oliveOil1)")
print("oliveOil2:\(oliveOil2)")

oliveOil1 = TestStruct.brutus(name: "Brutus Big ")
oliveOil2 = TestStruct.brutus(name: "Brutus Mean")
print("oliveOil1:\(oliveOil1)")
print("oliveOil2:\(oliveOil2)")

which results in these printouts:

foo1:TestStruct - func popeye - name:popeye sailorman Count:1
foo2:TestStruct - func popeye - name:popeye spinach   Count:1
foo1:TestStruct - func popeye - name:popeye sailorman Count:2
foo2:TestStruct - func popeye - name:popeye spinach   Count:1
foo1:TestStruct - func popeye - name:popeye sailorman Count:3
foo2:TestStruct - func popeye - name:popeye spinach   Count:1
foo1:TestStruct - func popeye - name:popeye sailorman Count:4
foo2:TestStruct - func popeye - name:popeye spinach   Count:2
oliveOil1:TestStruct - static func brutus - name:Brutus Big  count:1
oliveOil2:TestStruct - static func brutus - name:Brutus Mean count:1
oliveOil1:TestStruct - static func brutus - name:Brutus Big  count:1
oliveOil2:TestStruct - static func brutus - name:Brutus Mean count:1
app4g
  • 670
  • 4
  • 24

1 Answers1

0

var count = Int() // when declared within the static func
count = count + 1 // this never gets incremented

The above code you have declared inside the brutus(name function which is local var and has the same name with the class var. so the scope of the var is within the function. when the function end, count var inside the function also ends/release.

Each time function calls, you are creating a new count var so it always prints count 1.

You can not access class var inside the static function, because static function required static var too. so it's clear when you print inside the static function, it always used local var instead of class var.

What's the benefit of using static then? When do I use static func

By using static keyword also means, you no need to create an object for accessing var or function.

You can see some inbuilt function with static keyword for when you used.

Example: Int have one static function

@inlinable public static func random(in range: ClosedRange<Int>) -> Int

this function returns a random int value within the specified range.

so you can use the static function where you no need to create an object and use the direct function.

here is one example of the static function. You can see how I used the static function for showing the alert controllers. You can create your own static function where you don't want to create an object and then use function or var.

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52