Been searching SO and thru google for an hour. Haven't found an exact answer. Hence please verify my understanding.
Struct vs. Class
- Struct is by default preferred in swift.
- Struct is
value type
. Class isreference type
pic from : https://cocoacasts.com/value-types-and-reference-types-in-swift
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