当前位置: 首页 > news >正文

required 引发的小小思考

原创:转载请注明出处

 

 

首先,因为遇到问题如下:

class MainTabBar: UITabBar {

    override init(frame: CGRect) {

        super.init(frame: frame)

        self.translucent = false

        self.backgroundImage = UIImage(named: "tabbar")

    }

//    //在子类重写父类的必要构造器时,必须在子类的构造器前也添加required修饰符,表明该构造器要求也应用于继承链后面的子类。

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

 

    }

}

 

继承关系链:

UITabBar -> UIView -> 

public class UIView : UIResponder, NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment 

 

好,在UITabBar、UIView 中init方法并没有显示写出  Required

 

原因在此:

 

If you override any of a class's designated initializers, you don't inherit any other designated initializers. But UIView adopts the NSCoding protocol, which requires an init(coder:)initializer. So you must implement that one too:

required init(coder decoder: NSCoder) { super.init(coder: decoder) }

 

 

By the way: Xcode 会提示自动补全代码  

 

 

参考链接:

1.http://stackoverflow.com/questions/26081287/required-initializer-initcoder-must-be-provided-by-subclass-of-uitablevi

2.https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-XID_307

相关文章:

  • Python cos() 函数
  • [数据结构] 冒泡排序
  • NIPT无创产前分析思路
  • xshell、putty远程连接
  • 利用GitHub Pages和Hexo搭建个人博客
  • 为什么Python发展得如此之快?
  • 程序员为什么要时刻保持危机感?
  • 高并发服务设计——缓存
  • 关于领域模型与技术架构的关系的思考
  • 学会容器服务帮你打造Docker云端最佳运行环境
  • ASP.NET中 ListView(列表视图)的使用前台绑定
  • 1507 酒厂选址
  • win10系统的简单优化
  • Unity VR全景漫游
  • 查找、移除某个视图上的某类控件
  • 2017年终总结、随想
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • maya建模与骨骼动画快速实现人工鱼
  • oschina
  • SpringBoot 实战 (三) | 配置文件详解
  • SQLServer之创建数据库快照
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 猴子数据域名防封接口降低小说被封的风险
  • 检测对象或数组
  • 开发基于以太坊智能合约的DApp
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 前端设计模式
  • 使用Gradle第一次构建Java程序
  • 探索 JS 中的模块化
  • ionic入门之数据绑定显示-1
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • #### go map 底层结构 ####
  • #define
  • #我与Java虚拟机的故事#连载19:等我技术变强了,我会去看你的 ​
  • (poj1.3.2)1791(构造法模拟)
  • (二)JAVA使用POI操作excel
  • (二)正点原子I.MX6ULL u-boot移植
  • (二)丶RabbitMQ的六大核心
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (牛客腾讯思维编程题)编码编码分组打印下标题目分析
  • (十六)串口UART
  • (小白学Java)Java简介和基本配置
  • *** 2003
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .NET 8.0 中有哪些新的变化?
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .NetCore项目nginx发布
  • .sdf和.msp文件读取
  • @SuppressWarnings(unchecked)代码的作用
  • @Validated和@Valid校验参数区别
  • []串口通信 零星笔记
  • [2019/05/17]解决springboot测试List接口时JSON传参异常
  • [51nod1610]路径计数
  • [AIGC] Redis基础命令集详细介绍