類別&結構&列舉 Class & Structure & Enumeration


介紹

class 架構

class (名稱) {
}
class data {
    var text1 = "learning swift"
    var text2 = " is good."
}
var DATA = data()
print(DATA.text1 + DATA.text2)
  • 輸出結果
    learning swift is good.
    

標注型別

class data {
    var text1: String
    var text2: String
    init() {
        self.text1 = "learning swift"
        self.text2 = " is good."
    }
}
var DATA = data()
print(DATA.text1 + DATA.text2)
  • 輸出結果
    learning swift is good.
    

傳遞參數值

class data {
    var text1: String
    var text2: String
    init(text1: String, text2: String) {
        self.text1 = text1
        self.text2 = text2
    }
}
var DATA = data(text1: "learning swift", text2: " is good.")
print(DATA.text1 + DATA.text2)
  • 輸出結果
    learning swift is good.
    

加入條件

class data {
    var text1: String
    var text2: String
    init(text1: String, text2: String) {
        self.text1 = text1
        self.text2 = text2
        if text1.isEmpty || text2.isEmpty {
            self.text1 = "nothing"
            self.text2 = ""
        }
    }
}
var DATA = data(text1: "learning swift", text2: " is good.")
var DATA2 = data(text1: "", text2: "")
print(DATA.text1 + DATA.text2)
print(DATA2.text1 + DATA2.text2)
  • 輸出結果
    learning swift is good.
    nothing
    

類別繼承

class data {
    var text1: String
    var text2: String
    init(text1: String, text2: String) {
        self.text1 = text1
        self.text2 = text2
        if text1.isEmpty || text2.isEmpty {
            self.text1 = "nothing"
            self.text2 = ""
        }
    }
}
class data2: data {
}
let DATA = data2(text1: "learning swift", text2: " is good.")
print(DATA.text1 + DATA.text2)
  • 輸出結果
    learning swift is good.
    

插入函式

class data {
    var text1: String = "learning swift"
    var text2: String = " is good."
    func PRINT() {
        print(text1 + text2)
    }
}
var DATA = data()
    DATA.PRINT()
  • 輸出結果
    learning swift is good.
    

===&!==

class data {
    var text1: String
    var text2: String
    init() {
        self.text1 = "learning swift"
        self.text2 = " is good."
    }
}
var DATA1 = data()
var DATA2 = data()
var DATA3 = DATA1
var text1 = DATA1 === DATA2 ? "it is same class" : "it is different class"
print(text1)
var text2 = DATA3 === DATA1 ? "it is same class" : "it is different class"
print(text2)
  • 輸出結果
    it is different class
    it is same class
    

struct 架構

struct (名稱) {
}
struct data {
    var text1 = "learning swift"
    var text2 = " is good."
}
var DATA = data()
print(DATA.text1 + DATA.text2)
  • 輸出結果
    learning swift is good.
    
    • 與 class 用法相近,但不具繼承功能。

enum 架構

    enum (名稱) {
    case (值)
}
enum number: String {
    case one = "1"
    case two = "2"
    case three = "3"
}
var data = number.one
var data2: number = .two
print(data.rawValue)
print(data2.rawValue)
  • 輸出結果
    1
    2
    

enum & switch

enum number: String {
    case one
    case two
    case three
}
let data = number.one
switch data {
    case .one:
        print("one")
    case .two:
        print("two")
    case .three:
        print("three")
    default:
        break
}
  • 輸出結果
    one
    

關連值(associated value)

enum number {
    case one(String)
    case two(String)
    case three(String)
}
let data = number.one("1234567890")
switch data {
    case .one(let text):
        print("one, \(text)")
    case .two(let text):
        print("two, \(text)")
    case .three(let text):
        print("three, \(text)")
    default:
        break
}
  • 輸出結果
    one, 1234567890
    

原始值(rawValue)

enum number: String {
    case one = "1"
    case two = "2"
    case three = "3"
}
let data = number.one.rawValue
print(data)
  • 輸出結果
    1
    

results matching ""

    No results matching ""