• Swift


    (本文代码已升级至swift3)

    Swift语言的类扩展是一个强大的工具,我们可以通过类扩展完成如下事情:
    1,给已有的类添加计算属性和计算静态属性
    2,定义新的实例方法和类方法
    3,提供新的构造器
    4,定义下标脚本
    5,是一个已有的类型符合某个协议
    (注意:扩展只能添加新的计算型属性,不能添加存储型属性,也不能添加新的属性监视器)

    示例1:给字符串String类添加下标脚本,支持索引访问
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    extension String
    {
        subscript(start:Int, length:Int) -> String
            {
            get{
                let index1 = self.index(self.startIndex, offsetBy: start)
                let index2 = self.index(index1, offsetBy: length)
                let range = Range(uncheckedBounds: (lower: index1, upper: index2))
                return self.substring(with: range)
            }
            set{
                let tmp = self
                var s = ""
                var e = ""
                for (idx, item) in tmp.characters.enumerated() {
                    if(idx < start)
                    {
                        s += "(item)"
                    }
                    if(idx >= start + length)
                    {
                        e += "(item)"
                    }
                }
                self = s + newValue + e
            }
        }
        subscript(index:Int) -> String
            {
            get{
                return String(self[self.index(self.startIndex, offsetBy: index)])
            }
            set{
                let tmp = self
                self = ""
                for (idx, item) in tmp.characters.enumerated() {
                    if idx == index {
                        self += "(newValue)"
                    }else{
                        self += "(item)"
                    }
                }
            }
        }
    }
     
    var str = "hangge.com"
    print(str[7,3])
    print(str[7])
     
    str[7,3] = "COM"
    str[0] = "H"
     
    print(str[0,10])
    运行结果如下: 
    原文:Swift - 类扩展(extension)


    示例2:给Double增加mm,cm等方法,进行进制转换
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    extension Double
    {
        func mm()->String
        {
            return "(self/1)mm"
        }
        func cm()-> String
        {
           return "(self/10)cm"
        }
         
        func dm()->String{
            return "(self/100)dm"
        }
         
        func m()->String
        {
            return "(self/1000)m"
        }
        func km()->String
        {
            return "(self/(1000*1000))km"
        }
    }
     
    let value = 2000000000.0
    print(value.mm())
    print(value.cm())
    print(value.dm())
    print(value.m())
    print(value.km())
    运行结果如下:
    原文:Swift - 类扩展(extension)
  • 相关阅读:
    在qt中用tcp传输xml消息
    Response.Redirect 打开新窗体的两种方法
    div:给div加滚动栏 div的滚动栏设置
    高速排序算法
    海量数据处理面试题集锦
    VB中DateDiff 函数解释
    FusionCharts简单教程(一)---建立第一个FusionCharts图形
    mysql 加入列,改动列,删除列。
    Lucene教程具体解释
    Windows7下的免费虚拟机(微软官方虚拟机)
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7238133.html
Copyright © 2020-2023  润新知