Typealias dalam Swift


Kamu akan mempelajari cara membuat nama palsu / alias untuk apa saja. Oke karena langsung saja buka Playground Kamu karena typealias cukup singkat.

Typealias untuk String

Coba kita membuat alias untuk String.

typealias Name = String

func insertName(name: Name) {}
insertName(name: "Alif R")

Typealias untuk Custom Class

class Employee {}

typealias MyEmployees = [Employee]
func listEmployees(enterEmployees: MyEmployees) {}

Typealias untuk Tuple

typealias GridPoint = (Int, Int)

func enterPoint(grid: GridPoint) {
	print("You've entered, \(grid.0) and \(grid.1)")
}

enterPoint(grid: (4, 2))

Definisi Tipe

Coba kita tinjau bagaimana menginisialisasi Array, Dictionary, danOptional.

Tipe Array

let arrayOne: Array<String> = ["Budi", "Asep"] // Generic Struct
let arrayTwo: [String] = ["Budi", "Asep"]

Tipe Dictionary

let dictTwo: Dictionary<String, Int> = ["Panjul": 28, "Isal": 26] // Generic Struct
let dictOne: [String: Int] = ["Panjul": 28, "Isal": 26]

Tipe Optional

var optionalOne: String?
var optionalTwo: Optional<String> // Generic Enum

Semoga bermanfaat.

0 0 votes
Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

0
Would love your thoughts, please comment.x
()
x