最近需要建立UIButton的子类。

先看一看swfit中继承父类构造方法的条件:

Rule1 1
“If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers. Rule 2
If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.”

也就是说,如果你不在子类中实现任何designated 构造函数,就可以继承父类的所有designated 和 convience 构造函数。

再看一个复杂点的例子,这个例子是swfit的官方说明文档上的:

其中需要注意的就是第二个框中的内容

“The init(name: String) convenience initializer provided by RecipeIngredient takes the same parameters as the init(name: String) designated initializer from Food. Because this convenience initializer overrides a designated initializer from its superclass, it must be marked with the override modifier (as described in Initializer Inheritance and Overriding).

Even though RecipeIngredient provides the init(name: String) initializer as a convenience initializer, RecipeIngredient has nonetheless provided an implementation of all of its superclass’s designated initializers. Therefore, RecipeIngredient automatically inherits all of its superclass’s convenience initializers too.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l

这里可以看出,子类可以建立convinience 的构造方法去override 父类的designated 构造方法,也算实现了父类的designated 构造方法。

下面看具体的实验过程

1.先建立这个空的类,编译,无任何问题。

class TestBtn: UIButton {

}

2.继续添加

class TestBtn: UIButton {

    init(){

    }
}

这次编译后,有以下截图

为什么我不写 init() 方法就不需要实现 init(coder aDecoder: NSCoder) 方法呢,这里就设计到了开始提到的构造函数继承问题,由于我们的子类没有designated 构造方法,那么我们所写的这一个init 方法就被系统当做了designated 构造方法,所以我们的类不能继承UIButton的任何构造方法,而nscoding协议又要求UIButton实现这个构造方法,所以出现了警告。

3.我们消除警告后,代码如下,继续编译

class TestBtn: UIButton {

    init(){

    }

    required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} }

错误如下图

这个比较好理解,没有调用直接父类的designated 构造函数。

4. 更改代码如下,继续编译

class TestBtn: UIButton {

    init(){
super.init()
} required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} }

错误如下

就是说UIButton的init方法并不是一个 designated 构造方法。

UIButton无自定义init方法,因此是继承与UIControl的,而UIControl也无自定义init,因此是UIView的,而UIView的只有一个

init(frame: CGRect),那么它一定是一个designated 构造函数!

正确的写法如下:

class TestBtn: UIButton {

    init(){
super.init(frame: CGRectZero)
} required init(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
} }

ios swfit 由继承UIButton了解类的构造方法的更多相关文章

  1. 小白学开发(iOS)OC_ 使用继承来扩充类(2015-08-07)

    // //  main.m //  使用继承来扩充类 // //  Created by admin on 15/8/12. //  Copyright (c) 2015年 admin. All ri ...

  2. iOS学习——UI基础UIButton(七)

    前面写了UIWindow.UIViewController,那些都是一些框架,框架需要填充上具体的view才能组成我们的应用,移动应用开发中UI占了很大一部分,最基础的UI实现是使用系统提供的各种控件 ...

  3. IOS基础学习-2: UIButton

    IOS基础学习-2: UIButton   UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...

  4. IOS之UI -- 按钮UIButton的细节

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  5. ios学习总结(2) -- UIButton的使用

    原文地址 UIButton的类是一个UIControl子类,它实现了在触摸屏上的按钮.触摸一个按钮拦截事件和动作消息发送到目标对象时,它的挖掘.设定的目标和行动方法都继承自UIControl.这个类提 ...

  6. iOS 分类和继承

    iOS 中分类(Categories) 和 继承(Inherit)有相同的功能,但在一些细节上又有差异,简单介绍一下两者的异同. 分类可以在不知道系统类源代码的情况下,为这个类添加新的方法.分类只能用 ...

  7. iOS开发笔记系列-基础2(类)

    面向对象编程总是离不开类和对象的,Objective-C也不例外,不过Objective-C中的类还有一些自己的独特点. 类的声明和定义 在iOS开发中,类的声明与定义通常都是分开的,类得声明通常存放 ...

  8. iOS View自定义窍门——UIButton实现上显示图片,下显示文字

    “UIButton实现上显示图片,下显示文字”这个需求相信大家在开发中都或多或少会遇见.比如自定义分享View的时候.当然,也可以封装一个item,上边imageView,下边一个label.但是既然 ...

  9. 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)

    [源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...

随机推荐

  1. [c#基础]AutoResetEvent

    摘要 AutoResetEvent:msdn的描述是通知正在等待的线程已发生事件.此类不能被继承.也就是说它有那么一个时间点,会通知正在等待的线程可以做其它的事情了. AutoResetEvent 该 ...

  2. java的集合框架最全详解

    java的集合框架最全详解(图) 前言:数据结构对程序设计有着深远的影响,在面向过程的C语言中,数据库结构用struct来描述,而在面向对象的编程中,数据结构是用类来描述的,并且包含有对该数据结构操作 ...

  3. vmstat、top

    vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,使用vmstat命令可以得到关于进程.内存.内存分页.堵塞IO.traps及CPU活动的信息. vmstat 最常用的有两 ...

  4. [整理]Android开发(二)-Weather App

    private class WeatherData{ private String _weatherDescription; private Integer _currentTemperature; ...

  5. CATransition-转场动画

    CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果.iOS比Mac OS X的转场动画效果少一点 UINavigationController就是通过CATrans ...

  6. 大数据BI积累

    http://blog.csdn.net/wyzxg/article/category/535869 设计论文:http://www.doc88.com/p-3877368345851.html 自动 ...

  7. 素数的线性筛 && 欧拉函数

    O(n) 筛选素数 #include<bits/stdc++.h> using namespace std; const int M = 1e6 + 10 ; int mindiv[M] ...

  8. 大数据之Ganglia

    1.什么是ganglia 一个开源集群监视项目:Ganglia可以做系统监控,但是,目前它不支持服务器异常或故障报警功能. 2.Ganglia监控集群架构 Ganglia 集群主要是由gmond.gm ...

  9. 计算字符数组长度,用strlen 与 sizeof 的原理与区别

    遇到个坑,定义了一个字符数组 unsigned ;i<;i++) { buff[i] = ; } 然后用串口发送函数: write(fd, buff, strlen(buff)); 却发现串口一 ...

  10. Android 网络请求框架android-async-http问题

    今天通过接口请求服务器的一些app数据,发现一个很奇怪的问题,请求一个链接的时候,通常在第一次请求发起的时候没有什么问题,能很快的拿到数据,但是 往后再去请求的时候就会等待很久,而且最后会请求失败,一 ...