1.繼承於NSObject
1 class student: NSObject { 2 3 var name : String? 4 var age : Int = 0 5 var friend : Int = 0 6 7 init(name : String , age : Int , friend : Int) { 8 super.init() 9 self.name = name 10 self.age = age 11 self.friend = friend 12 } 13 14 init(dict : [String : AnyObject]) { 15 super.init() 16 setValuesForKeys(dict) 17 } 18 19 }
2.繼承於UIView
(1)系統默認初始化方法
class LyContentView: UIView { //系統默認初始化方法 override init(frame: CGRect) { super.init(frame: frame) //操作在這實現 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
(2)自定義初始化方法
注意:自定義初始化方法讀是調用 super.init(frame: frame),而不是super.init()
class LyContentView: UIView { fileprivate var name : String? fileprivate var age : Int init(frame: CGRect , name : String , age : Int) { self.name = name self.age = age //這里必須用init(frame:,而不是init super.init(frame: frame) //操作在這實現 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
3.UITableViewCell
import UIKit //cell的標識 let ID = "LyTableViewCell" class LyTableViewCell: UITableViewCell { class func cellWithTableView(_ tableView : UITableView) -> LyTableViewCell { var cell = tableView.dequeueReusableCell(withIdentifier: ID) as? LyTableViewCell if cell == nil { cell = UITableViewCell(style: .default, reuseIdentifier: ID) as? LyTableViewCell } return cell! } override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) //添加控件,一次性屬性讀在這實現 } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
4.UICollectionViewCell
import UIKit //cell的標識 private let ID = "LyCollectionViewCell" class LyCollectionViewCell: UICollectionViewCell { class func cellWithCollectionView(_ collectionView : UICollectionView , indexPath : IndexPath) -> LyCollectionViewCell { collectionView.register(LyCollectionViewCell.self, forCellWithReuseIdentifier: ID) return collectionView.dequeueReusableCell(withReuseIdentifier: ID, for: indexPath) as! LyCollectionViewCell } override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } }
5.便利構造器
extension UIColor { /* 類似於oc中的category/分類 1.convenience 2.最后別忘了調用自己一個默認的初始化方法self.init(.... */ convenience init(r : CGFloat, g : CGFloat, b : CGFloat) { self.init(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: 1.0) } }
6.構造方法規則
/* 規則 1 :如果子類沒有定義任何指定構造器,它將自動繼承所有父類的指定構造器。 規則 2 :如果子類提供了所有父類指定構造器的實現——無論是通過規則 1 繼承過來的,還是提供了自定義實現——它將 自動繼承所有父類的便利構造器。 即使你在子類中添加了更多的便利構造器,這兩條規則仍然適用 */ class Food { var name: String init(name: String) { self.name = name } convenience init() { self.init(name: "[Unnamed]") } } /* 1.如果RecipeIngredient沒有提供任何構造方法,那么它定義的屬性必須給初始值,它默認繼承Food的構造方法 */ class RecipeIngredient: Food { var quantity: Int = 0 init(name: String, quantity: Int) { self.quantity = quantity super.init(name: name) } override convenience init(name: String) { self.init(name: name, quantity: 1) } } let oneMysteryItem = RecipeIngredient() oneMysteryItem.quantity let oneBacon = RecipeIngredient(name: "Bacon") let sixEggs = RecipeIngredient(name: "Eggs", quantity: 6)
7.可失敗構造器
/* 1.在init后面+ ? 2.注意 :可失敗構造器的參數名和參數類型,不能與其它非可失敗構造器的參數名,及其參數類型相同。 3.注意 :成功不需要return */ struct Animal { let species: String init?(species: String) { if species.isEmpty { return nil } self.species = species } } class Person : NSObject { var name : String? var age : Int init?(age : Int) { if age < 0 { return nil } self.age = age super.init() } init(name : String , age : Int) { self.name = name self.age = age super.init() } } let p = Person(age: -1)//這里p為nil p?.age
8.使用kvo,dict -> model
class Person: NSObject { //1.要設置為可選類型,要不就設置初始值 var name : String? var age : Int = 0 init(dict : [String : AnyObject]) { super.init() setValuesForKeys(dict) } override func setValue(_ value: Any?, forKeyPath keyPath: String) { super.setValue(value, forKeyPath: keyPath) } override func setValue(_ value: Any?, forUndefinedKey key: String) { } }