• swift 闭包+嵌套函数+extension+单例+嵌套函数+??


     1 //: Playground - noun: a place where people can play
     2 
     3 import UIKit
     4 
     5 //*******************嵌套函数*****************************
     6 func getMathFunc(type:String) -> ((Int) -> Int) {
     7   func squre(num:Int) -> Int{
     8      return num * num
     9   }
    10   func cube(num:Int) -> Int{
    11      return num * num * num
    12   }
    13   switch (type) {
    14   case "squre":
    15     return squre
    16   default:
    17     return cube
    18   }
    19 }
    20 
    21 var mathFunc = getMathFunc("squre")
    22 mathFunc(4)
    23 var mathFunc2 = getMathFunc("other")
    24 mathFunc2(4)
    25 
    26 
    27 //********************闭包****************************
    28 //  {  (形参列表) -> 返回值类型 in
    29 //      可执行表达式
    30 //  }
    31 
    32 func getMathFunc1(type:String) -> ((Int) -> Int) {
    33   func squre(num:Int) -> Int{
    34     return num * num
    35   }
    36   func cube(num:Int) -> Int{
    37     return num * num * num
    38   }
    39   switch (type) {
    40   case "squre":
    41     return {(num:Int) -> Int in
    42         return num * num
    43      }
    44   default:
    45     return {(num:Int) -> Int in
    46        return num * num * num
    47     }
    48   }
    49 }
    50 var maxFunc3 = getMathFunc("squre")
    51 maxFunc3(5)
    52 var maxFunc4 = getMathFunc("other")
    53 maxFunc4(5)
    54 
    55 //
    56 var squre: (Int) -> Int = {(num) in return num * num}
    57 squre(3)
    58 
    59 
    60 //?? 如果??左边有值就就是原值,如果没有值那么就设置为??右边的值
    61 var a:Int?
    62 //a = 11
    63 print(a ?? 2)
    //: Playground - noun: a place where people can play
    
    import UIKit
    print("555")
    
    // MARK - guard
    func checkup(person:[String:String]){
      guard let id = person["id"] else {
        print("没有id,不能进入")
        return
      }
      guard let exam = person["exam"] else{
        print("没有exam,不能进入")
        return
      }
      print("id:(id),exam:(exam)--批准进入")
    }
    //checkup(["id":"123"])
    //checkup(["exam":"456"])
    checkup(["id":"123","exam":"456"])
    
    
    //MARK - 熟悉观察
    let MaxValue = 999
    let MinValue = -999
    var number = 0 {
       willSet{
       print("从(number)变为(newValue)")
       }
       didSet{
        if number > MaxValue {
           number = MaxValue
        }else if number < MinValue{
          number = MinValue
        }
        print("已经从(oldValue)变为(number)")
       }
    
    }
    number = 1000
    number
    
    
    //MARK - 扩展 extension
    //对Int扩展,增加一个方法
    extension Int {
      func times(closure:(() -> ())?){
        if self >= 0 {
          for _ in 0..<self {
            closure?()
          }
        }
      }
    }
    3.times{print("走起")}
    //MARK 协议扩展
    extension CustomStringConvertible{
      var upperDescription:String{
         return self.description.uppercaseString
      }
    }
    ["key":"value"].upperDescription
    
    //map:得到一个由闭包里面的返回值组成的新序列
    //flatMap:与map类似的功能,但是会过滤掉返回值里面的nil值
    //filter:得到一个由闭包返回值为true的值组成的新序列
    
    var result = [1,2,3,4,5].map{$0 * 2}
    result
    
    result = [1,2,3,4,5].filter{$0 > 2}
    result
    
    //MARK 单例
    class TestObject {
       static let testObject = TestObject()
      //私有构造,保证外部对象通过init方法创建单例类的其他实例
      private init() { }
    }

    源码下载:https://github.com/pheromone/swift-extension-

                http://download.csdn.net/detail/shaoting19910730/9515986

  • 相关阅读:
    redis 资料
    php 安装redis php扩展
    Unity生命周期
    疫情下的大学生人格发展研究
    对联一句——百花深处
    Unity实现byte[]合成图像
    Unity实现精灵资源动态加载
    数据结构与算法初步
    Unity中激活子物体
    C#实现自定义列表
  • 原文地址:https://www.cnblogs.com/shaoting/p/5479339.html
Copyright © 2020-2023  润新知