Swift - Generics
ภาษา Swift 4 มีคุณสมบัติ 'Generic' เพื่อเขียนฟังก์ชันและประเภทที่ยืดหยุ่นและใช้ซ้ำได้ Generics ใช้เพื่อหลีกเลี่ยงการทำซ้ำและเพื่อให้เป็นนามธรรม ไลบรารีมาตรฐาน Swift 4 สร้างขึ้นด้วยรหัสทั่วไป ประเภท 'Arrays' และ 'Dictionary' ของ Swift 4s เป็นของคอลเลกชันทั่วไป ด้วยความช่วยเหลือของอาร์เรย์และพจนานุกรมอาร์เรย์ถูกกำหนดให้เก็บค่า 'Int' และค่า 'String' หรือประเภทอื่น ๆ
func exchange(a: inout Int, b: inout Int) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("Before Swapping values are: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("After Swapping values are: \(numb1) and \(numb2)")
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
Before Swapping values are: 100 and 200
After Swapping values are: 200 and 100
ฟังก์ชันทั่วไป: ประเภทพารามิเตอร์
ฟังก์ชันทั่วไปสามารถใช้เพื่อเข้าถึงข้อมูลประเภทใดก็ได้เช่น 'Int' หรือ 'String'
func exchange<T>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("Before Swapping Int values are: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("After Swapping Int values are: \(numb1) and \(numb2)")
var str1 = "Generics"
var str2 = "Functions"
print("Before Swapping String values are: \(str1) and \(str2)")
exchange(a: &str1, b: &str2)
print("After Swapping String values are: \(str1) and \(str2)")
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
Before Swapping Int values are: 100 and 200
After Swapping Int values are: 200 and 100
Before Swapping String values are: Generics and Functions
After Swapping String values are: Functions and Generics
การแลกเปลี่ยนฟังก์ชัน () ใช้เพื่อสลับค่าซึ่งอธิบายไว้ในโปรแกรมข้างต้นและ <T> ใช้เป็นพารามิเตอร์ชนิด ในครั้งแรกการแลกเปลี่ยนฟังก์ชัน () ถูกเรียกให้ส่งคืนค่า 'Int' และการเรียกครั้งที่สองไปยังการแลกเปลี่ยนฟังก์ชัน () จะส่งคืนค่า 'สตริง' สามารถรวมพารามิเตอร์หลายประเภทไว้ในวงเล็บเหลี่ยมโดยคั่นด้วยเครื่องหมายจุลภาค
พารามิเตอร์ประเภทถูกตั้งชื่อตามที่ผู้ใช้กำหนดเพื่อให้ทราบวัตถุประสงค์ของพารามิเตอร์ type ที่เก็บไว้ Swift 4 ให้ <T> เป็นชื่อพารามิเตอร์ประเภททั่วไป อย่างไรก็ตามประเภทพารามิเตอร์เช่น Arrays และ Dictionaries ยังสามารถตั้งชื่อเป็นคีย์ค่าเพื่อระบุว่าเป็นของประเภท 'Dictionary'
struct TOS<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
}
var tos = TOS<String>()
tos.push(item: "Swift 4")
print(tos.items)
tos.push(item: "Generics")
print(tos.items)
tos.push(item: "Type Parameters")
print(tos.items)
tos.push(item: "Naming Type Parameters")
print(tos.items)
let deletetos = tos.pop()
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
[Swift 4]
[Swift 4, Generics]
[Swift 4, Generics, Type Parameters]
[Swift 4, Generics, Type Parameters, Naming Type Parameters]
การขยายประเภททั่วไป
การขยายคุณสมบัติสแต็กเพื่อให้ทราบว่าส่วนบนสุดของรายการจะรวมอยู่ในคีย์เวิร์ด 'extension'
struct TOS<T> {
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
}
var tos = TOS<String>()
tos.push(item: "Swift 4")
print(tos.items)
tos.push(item: "Generics")
print(tos.items)
tos.push(item: "Type Parameters")
print(tos.items)
tos.push(item: "Naming Type Parameters")
print(tos.items)
extension TOS {
var first: T? {
return items.isEmpty ? nil : items[items.count - 1]
}
}
if let first = tos.first {
print("The top item on the stack is \(first).")
}
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
["Swift 4"]
["Swift 4", "Generics"]
["Swift 4", "Generics", "Type Parameters"]
["Swift 4", "Generics", "Type Parameters", "Naming Type Parameters"]
The top item on the stack is Naming Type Parameters.
พิมพ์ข้อ จำกัด
ภาษา Swift 4 อนุญาตให้ 'ข้อ จำกัด ประเภท' ระบุว่าพารามิเตอร์ประเภทสืบทอดมาจากคลาสเฉพาะหรือเพื่อให้แน่ใจว่าเป็นไปตามมาตรฐานโปรโตคอล
func exchange<T>(a: inout T, b: inout T) {
let temp = a
a = b
b = temp
}
var numb1 = 100
var numb2 = 200
print("Before Swapping Int values are: \(numb1) and \(numb2)")
exchange(a: &numb1, b: &numb2)
print("After Swapping Int values are: \(numb1) and \(numb2)")
var str1 = "Generics"
var str2 = "Functions"
print("Before Swapping String values are: \(str1) and \(str2)")
exchange(a: &str1, b: &str2)
print("After Swapping String values are: \(str1) and \(str2)")
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
Before Swapping Int values are: 100 and 200
After Swapping Int values are: 200 and 100
Before Swapping String values are: Generics and Functions
After Swapping String values are: Functions and Generics
ประเภทที่เกี่ยวข้อง
Swift 4 อนุญาตให้มีการประกาศประเภทที่เกี่ยวข้องภายในนิยามโปรโตคอลด้วยคำหลัก 'ประเภทที่เกี่ยวข้อง'
protocol Container {
associatedtype ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
struct TOS<T>: Container {
// original Stack<T> implementation
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(item: T) {
self.push(item: item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> T {
return items[i]
}
}
var tos = TOS<String>()
tos.push(item: "Swift 4")
print(tos.items)
tos.push(item: "Generics")
print(tos.items)
tos.push(item: "Type Parameters")
print(tos.items)
tos.push(item: "Naming Type Parameters")
print(tos.items)
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
[Swift 4]
[Swift 4, Generics]
[Swift 4, Generics, Type Parameters]
[Swift 4, Generics, Type Parameters, Naming Type Parameters]
ที่ข้อ
ข้อ จำกัด ประเภทช่วยให้ผู้ใช้สามารถกำหนดข้อกำหนดเกี่ยวกับพารามิเตอร์ประเภทที่เกี่ยวข้องกับฟังก์ชันหรือประเภททั่วไป สำหรับการกำหนดข้อกำหนดสำหรับชนิดที่เกี่ยวข้องส่วนคำสั่ง 'where' ถูกประกาศเป็นส่วนหนึ่งของรายการพารามิเตอร์ type คีย์เวิร์ด 'where' ถูกวางไว้หลังรายการพารามิเตอร์ประเภทตามด้วยข้อ จำกัด ของประเภทที่เกี่ยวข้องความสัมพันธ์ที่เท่าเทียมกันระหว่างประเภทและประเภทที่เกี่ยวข้อง
protocol Container {
associatedtype ItemType
mutating func append(item: ItemType)
var count: Int { get }
subscript(i: Int) -> ItemType { get }
}
struct Stack<T>: Container {
// original Stack<T> implementation
var items = [T]()
mutating func push(item: T) {
items.append(item)
}
mutating func pop() -> T {
return items.removeLast()
}
// conformance to the Container protocol
mutating func append(item: T) {
self.push(item: item)
}
var count: Int {
return items.count
}
subscript(i: Int) -> T {
return items[i]
}
}
func allItemsMatch<
C1: Container, C2: Container
where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>
(someContainer: C1, anotherContainer: C2) -> Bool {
// check that both containers contain the same number of items
if someContainer.count != anotherContainer.count {
return false
}
// check each pair of items to see if they are equivalent
for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
// all items match, so return true
return true
}
var tos = Stack<String>()
tos.push(item: "Swift 4")
print(tos.items)
tos.push(item: "Generics")
print(tos.items)
tos.push(item: "Where Clause")
print(tos.items)
var eos = ["Swift 4", "Generics", "Where Clause"]
print(eos)
เมื่อเรารันโปรแกรมข้างต้นโดยใช้สนามเด็กเล่นเราจะได้ผลลัพธ์ดังนี้ -
[Swift 4]
[Swift 4, Generics]
[Swift 4, Generics, Where Clause]
[Swift 4, Generics, Where Clause]