QSD的Swift妙妙屋3:妙妙的理論之6種Type

struct & class


Both struct and class have:

1. var
var body: some View {
    Text("Hellow World!")
}
2. let
let defaultColor = Color.orange
3. functions
func multiply(operand: Int, by: Int) -> Int {
    return operand * by
}
multiply(operand: 5, by: 6)
  • 注意:每個parameter可以有2個label:
func multiply(_ operand: Int, by otherOperand: Int) -> Int {
   return operand * otherOperand
}
multiply(5, by: 6)

第一個label給外部輸入用;第二個label給func內(nèi)部用。

  • _ means "unused" / "leave it out".
4. initializers
  • Special functions that are called when creating a struct or class.
struct MemoryGame {
    init (numberOfParisOfCards: Int) {
        // create a game with that many paris of cards.
    }
}
  • 一個struct中可以有很多個init,每個init攜帶不同參數(shù)。

Difference between struct and class

  • struct is a Value type, but class is a Reference type.
    struct class
    Value type Reference type
    copied when passed or assigned passed around via pointers in heap
    copy on write automatically reference counted
    functional programming object-oriented programming
    no inheritance inheritance (single)
    "free" init initializes ALL vars "free" init initializes NO vars
    Mutability explicitly stated Always mutable
  • Most things you see are struct. Arrays, Bools, Dictionaries...
  • struct is your "go-to" data structure, whereas class is only used in specific circumstances.
  • ViewModel in MVVM is always a class.
  • UIKit (old-style iOS) is class-based.

protocol

  • View is a protocol.

type parameter (don't care)

  • Sometimes we may want to manipulate data structures that we are "type agnostic" about. In other words, we do not know what type it is and we do not care.

  • Swift is a strongly-typed language, so we cannot have variables that are "untyped". Therefore, we use type parameter.

  • An awesome example of generics: Array.

How Array uses a "don't care" type

struct Array<Element> {
...
func append(_ element; Element) {...}
}
  • The type of the argument to append is Element: a "don't care" type.
  • The Element is determined when someone uses Array.
var a = Array<Int>()
a.append(5)

enum


functions

  • Functions are also types.
var operation: (Double) -> Double

This is a var called operation. It is of type "function that takes a Double and returns a Double".

func square(operand: Double) -> Double {
    return operand * operand
}
operation = square
let result = operation(4) // result would equal 16
operation = sqrt //sqrt is a built-in function
  • We do not use argument labels (e.g. operand:) when executing function types.

Closures

When passing functions around that we are very often inlining them, we call such an inlined function a closure


SwiftUI is mostly about functional programming, therefore "functions as types" is a very important concept in Swift.

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容