Swift语法知识小记

作者: 蠢萌的L君 | 来源:发表于2017-04-11 09:33 被阅读134次
    • 不让动画自动停止方法
    • 隐藏界面会让界面上的动画消失。
    let anim = CABasicAnimation(keyPath:"transfrom.rotation")
         
    //    设置结束后不移除动画
    anim.isRemovedOnCompletion = false
    
    • 判断文本输入框不为空格以及空
    func spaceStringAndEmptyString() -> Bool{
            let str = self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            if str.characters.count == 0{
                return true // 为空
            }else {
                return false // 不为空
            }
        }
        
    
    • 颜色转RGB方法
    func colorFromARGB(_ value: Int32) -> UIColor {
            let alpha = CGFloat(((0xff << 24) & value) >> 24) * 1.0 / 255.0
            let red = CGFloat(((0xff << 16) & value) >> 16) * 1.0 / 255.0
            let green = CGFloat(((0xff << 8) & value) >> 8) * 1.0 / 255.0
            let blue = CGFloat(0xff & value) / 255.0
            
            return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
        
    func colorFromRGB(_ value: Int32) -> UIColor {
            let red = CGFloat(((0xff << 16) & value) >> 16) * 1.0 / 255.0
            let green = CGFloat(((0xff << 8) & value) >> 8) * 1.0 / 255.0
            let blue = CGFloat(0xff & value) / 255.0
            
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
        
    func colorFromRGB(_ value: Int32, withAlpha alpha: CGFloat) -> UIColor {
            let red = CGFloat(((0xff << 16) & value) >> 16) * 1.0 / 255.0
            let green = CGFloat(((0xff << 8) & value) >> 8) * 1.0 / 255.0
            let blue = CGFloat(0xff & value) / 255.0
            
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
    }
    
    
    
    • 函数作为返回值
    func makeIncrementer() -> ((Int) -> Int) {
        func addOne(number: Int) -> Int {
            print(number)
            return 1 + number
        }
        return addOne
    }
    
    var increment = makeIncrementer() // 这里将addOne函数返回回来
    
    increment(8) // 实际调用addOne函数
    
    • 闭包的使用
    var numbers = [20, 19, 7, 12]
    
    numbers.map({
        (number: Int) -> Int in
        let result = 3 * number
        return result
    })
    
    
    • 关于swift抛出错误与处理
    // 错误的枚举
    enum PrinterError: Error {
        case outOfPaper
        case noToner
        case onFire
    }
    
    
    func send(job: Int, toPrinter printerName: String) throws -> String {
        if printerName == "Never Has Toner" {
            // 在这里如果条件成立,抛出错误
            throw PrinterError.noToner
        }
        return "Job sent"
    }
    
    // 在do中 try可能出错的函数,如果throw出错误,就在catch中处理错误
    do {
        let printerResponse = try send(job: 1040, toPrinter: "Never Has Toner")
        print(printerResponse)
    } catch {
        print(error)
    }
    
    // 也可以用这种方法来try  如果抛出错误,则返回nil,否者返回函数返回值
    let printerSuccess = try? send(job: 1884, toPrinter: "Mergenthaler")
    let printerFailure = try? send(job: 1885, toPrinter: "Never Has Toner")
    
    
    
    • 枚举的高级用法
    enum Suit {
        case Spades, Hearts, Diamonds, Clubs
        func simpleDescription() -> String {
            switch self {
            case .Spades:
                return "spades"
            case .Hearts:
                return "hearts"
            case .Diamonds:
                return "diamonds"
            case .Clubs:
                return "clubs"
            }
        }
    }
    
    // 设置枚举的属性
    let hearts = Suit.Hearts
    // 根据枚举属性不同调用不同方法
    let heartsDescription = hearts.simpleDescription()
    
    
    • 关于defer

    使用defer代码块来表示在函数返回前,函数中最后执行的代码。无论函数是否会抛出错误,这段代码都将执行。使用defer,可以把函数调用之初就要执行的代码和函数调用结束时的扫尾代码写在一起,虽然这两者的执行时机截然不同。

    var fridgeIsOpen = false
    let fridgeContent = ["milk", "eggs", "leftovers"]
    
    func fridgeContains(_ food: String) -> Bool {
        fridgeIsOpen = true
        defer {
            fridgeIsOpen = false
        }
        
        let result = fridgeContent.contains(food)
        return result
    }
    fridgeContains("banana")
    print(fridgeIsOpen)
    
    • swift中泛型
    func repeatItem<Item>(repeating item: Item, numberOfTimes: Int) -> [Item] {
        var result = [Item]()
        for _ in 0..<numberOfTimes {
            result.append(item)
        }
        return result
    }
    repeatItem(repeating: "knock", numberOfTimes:4) // 结果为["knock","knock","knock","knock"]
    

    相关文章

      网友评论

        本文标题:Swift语法知识小记

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