美文网首页
关键字学习

关键字学习

作者: 太逸 | 来源:发表于2016-04-18 16:32 被阅读0次

convenience:

用在init方法之前。所有的convenience初始化方法都必须调用同一个类中的 designated 初始化完成设置,另外convenience的初始化方法是不能被子类重写或者是从子类中以super的方式被调用的。

class ClassA {
            let numA: Int
            init(num: Int) {
                numA = num
            }
            convenience init(bigNum: Bool) {
                self.init(num: bigNum ? 10000 : 1)
            }
        }
        class ClassB: ClassA {
            let numB: Int
            
            override init(num: Int) {
                numB = num + 1
                super.init(num: num)
            }
        }

只要在子类中实现重写了父类 convenience方法所需要的 init方法的话,我们在子类中就也可以使用父类的 convenience初始化方法了

guard

判断属性是否存在,如果不存在,可以做一些操作

        func apply() -> Bool {
            
            guard let image = UIImage(named:"some")
                else { return false }
            
            ...
            
        }
        

这里的 guard关键字,判读了 UIImage 是否创建成功,如果没有创建成功,在 else 分支中会将函数直接返回。

where

where
关键字的用处非常广泛,在 switch语句,catch分支语句或者条件语句比如 if, while, guard, for-in或者来限制类型的时候。
for-in

    let arr = [1,2,3,4]
    let dict = [1: "one", 2: "two"]
    
    for num in arry where dict[num] != nil {
    //1, 2
    }

do-catch

    enum ResponseError: ErrorType {
        case HTTP(Int)
    }
    
    func errorProne() throws {
        throw ResponseError.HTTP(404)
    }
    
    do {
    try errorProne()
    }catch ResponseError.HTTP(let code) where code >= 400 && code % 2 == 0 {
    print("Bad Request") //match
    }catch ResponseError.HTTP(let code) where code >= 50 && code < 600 {
    print("Internal Server Error")
    }
}

while

    var mutableArray: [Int]? = []
    while let arr = mutableArray  where arr.count < 5 {
    mutableArray?.append(0) //[0,0,0,0,0]
    }

if

    let string: String? = "checkmate"
    if let str = string where str != "checkmate"{
        print("game over")
    }else {
        print("Lte's play")
    }

guard

    let string: String? = "checkmate"
    guard let str = string where str != "checkmate" else{
        fataError("game over")
    }
    print("Let's play")

switch-case

    var value = (1, 2)
    switch value {
    case let (x, y) where x == 1:
    //
    case let (x, y) where x / 5 = 1:
    //
    default:
    
    }

type constraint

    func genericFunction<S where S: StringLiteralConvertible>(string: S) {
        print(string)
    }
    genericFunction("lambada")

相关文章

  • RobotFramework学习02-基础关键字

    RobotFramework学习笔记目录 本文包含内容: 基础关键字 基础关键字(高级用法) 内置关键字查询 关键...

  • 第三次作业:思维导图阅读法

    重点: 关键字一:主动学习 学习最有效的方式是主动学习,主动学习最有效的方式是训练他人。 关键字二:习 习指练习、...

  • Java中的this关键字

    学习笔记:thisthis关键字修饰的变量指代成员变量 1、this关键字this关键字修饰的变量指代成员变量 方...

  • this 关键字的理解--java学习笔记

    this 关键字的理解--java学习笔记 彻底理解this 关键字的含义 this关键字再java里面是一个我认...

  • 变量与常量

    变量与常量 @(Java学习) 一、关键字 Java语言中具有特殊用途的词称为关键字。 Java中常用的关键字如下...

  • java高并发解决方案-----synchronized

    1.什么的synchronize 今天学习了java的关键字synchronized关键字synchronized...

  • 关键字学习

    convenience: 用在init方法之前。所有的convenience初始化方法都必须调用同一个类中的 de...

  • 关键字学习

    特殊语法 CONCATENATE FIND “匹配相同的字符 导入内表 "找到匹配的位置 和 长度 ”正则表达式 ...

  • robotframework框架学习(一)

    常用关键字的学习 一些是Builtln关键字,一些是selenium2Library关键字 1. Log功能是打印...

  • iOS属性关键字

    iOS属性关键字 引言 学习 iOS 开发的人,大多都绕不开属性关键字—— assign,weak,unsafe_...

网友评论

      本文标题:关键字学习

      本文链接:https://www.haomeiwen.com/subject/oodalttx.html