1.继承于NSObject

 class student: NSObject {

     var name : String?
var age : Int =
var friend : Int = init(name : String , age : Int , friend : Int) {
super.init()
self.name = name
self.age = age
self.friend = friend
} init(dict : [String : AnyObject]) {
super.init()
setValuesForKeys(dict)
} }

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 =
init(name: String, quantity: Int) {
self.quantity = quantity
super.init(name: name)
}
override convenience init(name: String) {
self.init(name: name, quantity: )
}
} let oneMysteryItem = RecipeIngredient()
oneMysteryItem.quantity let oneBacon = RecipeIngredient(name: "Bacon")
let sixEggs = RecipeIngredient(name: "Eggs", quantity: )

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 < { return nil }
self.age = age super.init()
} init(name : String , age : Int) {
self.name = name
self.age = age
super.init()
}
} let p = Person(age: -)//这里p为nil
p?.age

8.使用kvo,dict -> model

class Person: NSObject {

    //1.要设置为可选类型,要不就设置初始值
var name : String?
var age : Int = 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) { }
}

初始化方法,init,构造器的更多相关文章

  1. OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏

    继承: 1.单向继承,一个类只能有一个父类,一个父类可以有多个子类. 2.单向继承,基类(根类)是OSObject 3.子类可以继承父类的属性和方法 当父类的方法不满足子类的需求时,子类可以重写父类的 ...

  2. Object_C初始化方法, 遍历构造器

    //版本1 //- (id)init //{ //    work = @"工作"; //    return self; //} // //    //版本2:调用父类的init ...

  3. Objective-C学习笔记(二十二)——初始化方法init的重写与自己定义

    初学OC.对init这种方法不是非常了解.我们如今来分别对init方法进行重写以及自己定义,来加深对他的了解. 本样例也是用Person类来进行測试. (一)重写init方法. (1)在Person. ...

  4. 【iOS】swift init构造器

    这几天在使用 Swift 重写原来的一个运动社交应用 SportJoin. 为什么要重写呢? 首先因为实在找不到设计师给我作图; 其次, 我也闲不下来, 想找一些项目做, 所以只好将原来的代码重写了. ...

  5. python面向对象的基础语法(dir内置函数、self参数、初始化方法、内置方法和属性)

    面相对象基础语法 目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属性 01. dir 内置函数(知道) 在 Python 中 对象几乎是无所不在的 ...

  6. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  7. OC:继承、初始化方法、便利构造器

    A继承 的作用就是为了,节省代码.     继承 :子类会继承父类 里面所有的内容     思想 :凡是继承过来的实例变量和方法 都是自己的(是从父类遗传过来的)     继承的使用场景 : 当我们多 ...

  8. Swift - 类初始化和反初始化方法(init与deinit)

    1,init():类的初始化方法(构造方法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  9. oc实例变量初始化方法

    1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...

随机推荐

  1. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  2. 微信H5支付,成功样例

    <?php/** * Created by PhpStorm. * User: Administrator * Date: 2019/6/3 * Time: 12:00 */ if( !defi ...

  3. runlevel 运行级别

    linux启动过程 关于Ubuntu 12.04修改默认运行级别,启动字符界面的个人理解   网上通常的做法是:(亲自试验,不管用),如果想直接操作请看绿色字体部分 (1)第一种方法:   由于Red ...

  4. Python实现Collatz序列(考拉兹猜想)

    考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想.3n+1猜想.冰雹猜想.角谷猜想.哈塞猜想.乌拉姆猜想或叙拉古猜想,是指对于每一个正整数,如果它是奇数,则对它乘3再加1, ...

  5. vue-router HTML5 History 模式(转自官网)

    vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...

  6. 去掉select在苹果手机上的原生样式

    outline: none; -webkit-appearance: none; 该属性会去掉select所有的默认样式,包括下拉箭头,因此需要通过额外的样式控制下拉箭头

  7. 一文彻底搞懂Cookie、Session、Token到底是什么

    > 笔者文笔功力尚浅,如有不妥,请慷慨指出,必定感激不尽 Cookie 洛:大爷,楼上322住的是马冬梅家吧? 大爷:马都什么? 夏洛:马冬梅. 大爷:什么都没啊? 夏洛:马冬梅啊. 大爷:马什 ...

  8. 豆瓣爬虫Scrapy“抄袭”改写

    主要是把项目从docker里面扒拉出来,但是扒拉完好像又没有什么用,放在docker里面运行多好. 源码下载下面主要记一下改动的地方吧. 配置:在database.py中改掉自己的数据库配置. 表结构 ...

  9. php中openssl_pkey_get_private()函数遇到false的问题 解决办法

    今天用openssl_pkey_get_private()函数遇到了一个大坑: 如果你的私钥文件(private_key.pem)是 -----BEGIN PRIVATE KEY-----字符串字符串 ...

  10. caffe不同lr_policy参数设置方法

    fixed 参数: base_lr: 0.01 lr_policy: "fixed" max_iter: 400000 step 参数: base_lr: 0.01 lr_poli ...