你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift_初始化

Swift_初始化

編輯:IOS開發綜合

本文將為大家說明Swift_初始化的文章,具體方法請看介紹

#Swift_初始化

點擊查看源碼

初始化結構體
//初始化結構體
func testInitStruct() {
    //結構體 類中默認方法
    struct Size {
        //寬
        var width = 0.0
        //高
        var height = 0.0
    }
    
    //默認初始化 使用的是init()構造方法
    var size = Size()
    //結構體自帶根據成員初始化結構體的功能
    size = Size(width: 10, height: 10)
    print(size)
    
    struct Point {
        
        var x = 0.0, y = 0.0
        
        //默認初始化
        init() {
            
        }
        
        //通過成員初始化結構體
        init(x:Double, y:Double) {
            self.x = x
            self.y = y
        }
    }
    
    //自定義初始化
    var point = Point() //調用inin()方法
    point = Point(x: 10, y: 10) //調用init(x: Double, y:Double)方法
    print(point)
    
    /*  print
     
     (Size #1)(width: 10.0, height: 10.0)
     (Point #1)(x: 10.0, y: 10.0)
     
     */
    
}
初始化枚舉
//初始化枚舉
func testInitEnum() {
    
    enum CompassPoint:String {
        
        case North, South, East, West
        
        init(symbol: String) {
            switch symbol {
            case "North":
                self = .North
            case "South":
                self = .South
            case "East":
                self = .East
            default:
                self = .West
            }
        }
        
    }
    
    //直接取值
    var compassPoint = CompassPoint.West
    print(compassPoint)
    //通過原始值初始化
    compassPoint = CompassPoint(rawValue: "North")!
    print(compassPoint)
    //通過自定義的初始化方法
    compassPoint = CompassPoint(symbol: "North")
    print(compassPoint)
    
    /*  print
     
     West
     North
     North
     
     */
}
初始化類
//初始化類
func testInitClass() {
    
    class BaseClass {
        
        var name: String? //可選屬性類型 可能為String或nil
        
        init() {
            print("Food:init()")
        }
        
        convenience init(name: String) {
            self.init()
            self.name = name
        }
        
        //省略外部參數
        convenience init(_ subName: String) {
            self.init(name: subName)
        }
    }
    
    class SubClass: BaseClass {
        
        override init() {
            super.init() //實現父類的init()方法
        }
        
    }
    
    //默認初始化
    var user = SubClass()
    //通過屬性初始化
    user = SubClass(name: "XuBaoAiChiYu")
    //使用init(_ name: String)初始化
    user = SubClass("XuBaoAiChiYu")
//    var name = ""
//    name = user.name //因為name可以為nil。此處會報錯,
    if let name = user.name {
        print(name)
    }
    
    /*  print
     
     Food:init()
     Food:init()
     Food:init()
     XuBaoAiChiYu
     
     */
}
結構體初始化失敗
//結構體初始化失敗
func testFailableInitStruct() {
    struct Animal {
        let species: String
        init?(species: String) {
            if species.isEmpty { return nil }
            self.species = species
        }
    }
    let someCreature = Animal(species: "Giraffe")
    //someCreature is of type Animal?, not Animal
    if let giraffe = someCreature {
        print("\(giraffe.species)")
    }
    
    /*  print
     
     Giraffe
     
     */
}
枚舉初始化失敗
//枚舉初始化失敗
func testFailableInitEnum() {
    
    enum TemperatureUnit:Character {
        
        case kelvin = "K", celsius = "C", fahrenheit = "F"
        
        init?(symbol: Character) {
            switch symbol {
            case "K":
                self = .kelvin
            case "C":
                self = .celsius
            case "F":
                self = .fahrenheit
            default:
                return nil
            }
        }
        
    }
    //通過自定義方法初始化
    var unit = TemperatureUnit(symbol: "F")
    print(unit!) //Fahrenheit
    unit = TemperatureUnit(symbol: "X")
    print(unit) //nil
    
    //通過原始值初始化
    unit = TemperatureUnit(rawValue: "F")
    print(unit!) //Fahrenheit
    unit = TemperatureUnit(rawValue: "X")
    print(unit) //nil
    
    /*
     
     fahrenheit
     nil
     fahrenheit
     nil
     
     */
    
}
類初始化失敗
//類初始化失敗
func testFailableInitClass() {
    
    class Product {
        
        var name: String?
        
        init() {}
        
        init?(name: String) {
            self.name = name
            if name.isEmpty {
                return nil
            }
        }
    }
    //可選鏈操作 當bowTie為真時 執行內部代碼
    if let qq = Product(name: "1045214799") {
        print(qq.name!)
    }
    
    /*
     
     Optional("1045214799")
     
     */
}
必須初始化
//必須初始化
func testRequiredInit() {
    
    class SomeClass {
        
        required init() {
            //子類要調用此方法,必須繼承實現
        }
        
    }
    
    class SomeSubclass: SomeClass {
        
        required init() {
            
        }
        
        init(required:String) {
            super.init()
            //這裡調用父類的init()方法 當前類必須實現init()方法
        }
    }
}
使用閉包或函數設置默認屬性
//使用閉包或函數設置默認屬性
func testSettingDefaultPropertyValueWithClosureOrFunction() {
    class SomeClass {
        
        let someProperty: String = {
            //create a default value for someProperty inside this closure
            //someValue must be of the same type as SomeType
            return "someValue"
        }()
    }
    
    let c = SomeClass()
    print(c.someProperty)
    
    /*  print
     
     someValue
     
     */
    
}

【Swift_初始化】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved