初始化方法,init,构造器
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,构造器的更多相关文章
- OC基础:继承.初始化方法,便利构造器 分类: ios学习 OC 2015-06-16 19:27 84人阅读 评论(0) 收藏
继承: 1.单向继承,一个类只能有一个父类,一个父类可以有多个子类. 2.单向继承,基类(根类)是OSObject 3.子类可以继承父类的属性和方法 当父类的方法不满足子类的需求时,子类可以重写父类的 ...
- Object_C初始化方法, 遍历构造器
//版本1 //- (id)init //{ // work = @"工作"; // return self; //} // // //版本2:调用父类的init ...
- Objective-C学习笔记(二十二)——初始化方法init的重写与自己定义
初学OC.对init这种方法不是非常了解.我们如今来分别对init方法进行重写以及自己定义,来加深对他的了解. 本样例也是用Person类来进行測试. (一)重写init方法. (1)在Person. ...
- 【iOS】swift init构造器
这几天在使用 Swift 重写原来的一个运动社交应用 SportJoin. 为什么要重写呢? 首先因为实在找不到设计师给我作图; 其次, 我也闲不下来, 想找一些项目做, 所以只好将原来的代码重写了. ...
- python面向对象的基础语法(dir内置函数、self参数、初始化方法、内置方法和属性)
面相对象基础语法 目标 dir 内置函数 定义简单的类(只包含方法) 方法中的 self 参数 初始化方法 内置方法和属性 01. dir 内置函数(知道) 在 Python 中 对象几乎是无所不在的 ...
- Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)
原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...
- OC:继承、初始化方法、便利构造器
A继承 的作用就是为了,节省代码. 继承 :子类会继承父类 里面所有的内容 思想 :凡是继承过来的实例变量和方法 都是自己的(是从父类遗传过来的) 继承的使用场景 : 当我们多 ...
- 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 ...
- oc实例变量初始化方法
1 使用实例setter方法 默认初始化方法 + setName:xxx setAge:xxx 2 使用实例功能类方法,默认初始化方法 + setName:xxx age:xxx3 使用实例初始化方法 ...
随机推荐
- LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)
题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...
- Xcode7以后访问http加密
Xcode7 在Info.plist中add Row添加NSAppTransportSecurity类型Dictionary. 在NSAppTransportSecurity下添加NSAllowsAr ...
- 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...
- SaltStack中状态间关系unless、onlyif、require、require_in、watch、watch_in
1.unless 检查的命令,仅当unless选项指向的命令返回值为false时才执行name定义的命令 cmd.run: {% "] %} - name: 'nohup sh /alida ...
- Jupyer Notebook, Jupyter Lab 虚拟环境配置
虚拟环境 conda create -n python36 python=3.6 使用以下命令激活: activate python36 Notebook 安装插件 conda install nb_ ...
- [mark]C# 异常处理
https://docs.microsoft.com/zh-cn/dotnet/articles/csharp/programming-guide/exceptions/index
- Halcon中将16位的图像转化为8位的图像
Halcon中Image有多种像素表示方式,这方面网上找到的资料比较少,有一张大恒图像培训的文档中提到过,感觉描述比较准确: 里面有四种类型比较类似:uint2.int1.int2.int4. 区分起 ...
- jq轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- liunx常用命令笔记
安装软件教程 linux安装java:https://www.cnblogs.com/lamp01/p/8932740.html linux安装mysql:https://www.cnblogs.co ...
- goweb-表单
表单 简单的处理一个登陆界面 package main import ( "fmt" "html/template" "log" " ...