Swift - 下标脚本方法介绍及实例

定义下标脚本之后,可以使用“[]”来存取数据类型的值。

示例1:实现一个我们自定的字符串类,可以方便的通过索引获取某一个字符值,或某一部分字符串。同时也可以通过索引,给某一部分赋值。
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
55
56
57
class SubString
{
    var str:String = ""
    init(str:String)
    {
        self.str = str;
    }
     
    /**下标脚本:获取/设置部分字符串**/
    subscript(start:Int, length:Int) -> String
    {
        get{
            return (str as NSString).substringWithRange(NSRange(location: start, length: length))
        }
        set{
            var tmp = Array(str)
            str = ""
            var s = ""
            var e = ""
            for (idx, item) in enumerate(tmp) {
                if(idx < start)
                {
                    s += "\(item)"
                }
                if(idx >= start + length)
                {
                    e += "\(item)"
                }
            }
            str = s + newValue + e
        }
    }
     
    /**下标脚本:获取/设置字符**/
    subscript(index:Int) -> String
    {
        get{
            return String(Array(str)[index])
        }
        set{
            var tmp = Array(str)
            tmp[index] = Array(newValue)[0]
            str = ""
            for (idx, item) in enumerate(tmp) {
                str += "\(item)"
            }
        }
    }
}
 
var str = SubString(str:"hangge.com")
println(str[7,3])  //获取字符串:com
println(str[7])  //获取字符:c
 
str[7,3] = "COM"  //设置部分字符串
str[0] = "H" //设置部分字符
println(str[0,10])

示例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
extension String
{
    subscript(start:Int, length:Int) -> String
        {
        get{
            return (self as NSString).substringWithRange(NSRange(location: start, length: length))
        }
        set{
            var tmp = Array(self)
            var s = ""
            var e = ""
            for (idx, item) in enumerate(tmp) {
                if(idx < start)
                {
                    s += "\(item)"
                }
                if(idx >= start + length)
                {
                    e += "\(item)"
                }
            }
            self = s + newValue + e
        }
    }
    subscript(index:Int) -> String
        {
        get{
            return String(Array(self)[index])
        }
        set{
            var tmp = Array(self)
            tmp[index] = Array(newValue)[0]
            self = ""
            for (idx, item) in enumerate(tmp) {
                self += "\(item)"
            }
        }
    }
}
 
var str = "hangge.com"
println(str[7,3])
println(str[7])
 
str[7,3] = "COM"
str[0] = "H"
 
println(str[0,10])

示例2:使用一维数组结合下标方法一定程度上模拟实现了二维数组

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
class Matrix {
    let rows: Int, columns: Int
    var grid: [Double]
     
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(count: rows * columns, repeatedValue: 0.0)
    }
     
    func indexIsValidForRow(row: Int, column: Int) -> Bool {
        return row >= 0 && row < rows && column >= 0 && column < columns
    }
     
    subscript(row: Int, column: Int) -> Double {
        get {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            return grid[(row * columns) + column]
        }
        set {
            assert(indexIsValidForRow(row, column: column), "Index out of range")
            grid[(row * columns) + column] = newValue
        }
    }
}
 
var value = Matrix(rows: 20,columns: 20)
value[10,10] = 20
println(value)
上一篇:vagrant初始登录失败的一般性解决方案


下一篇:laravel homestead vagrant box安装使用,问题,及相关命令