objective-cproperty方法支持assign/readonly/retain/readwrite等。 swift也支持类似这些属性。

一.swift属性方法

1.readonly

1
2
3
4
5
6

var name:String{
    get{
        return self.name
    }
}

2.read/write with backing store如果发生EXC_BAD_ACCESS

参考http://stackoverflow.com/questions/24025340/property-getters-and-setters

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

//read/write Setters/Getters用来计算和获取属性,
//并没有给成员变量实例分配存储,所以,会触发EXC_BAD_ACCESS.
private var realAge:Int = 0;

var age:Int{
    get{
        return realAge
    }
    
    set{
        realAge = newValue
    }
}

3.read/write with backing store,信息隐藏,监控属性变化,附加额外处理类似

1
2
 
 @property(nonatomic, retain)NSString address;

实例变量自动为_address

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

private var _address :String = ""{
    willSet{

          println("old Value = \(_address) will clear,new value = \(newValue) will set")
        
        self._address = newValue;
    }
    didSet{
        //可以追回一些额外的处理逻辑
         println("Property did set")
    }
}

var address :String{
    get{
        return self._address;
    }
    set(newAddress){
        if newAddress != _address{
            self._address = newAddress
        }
    }
}

二.完整示例

1.QBaseClass overview

 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71

class QBaseClass{
    
    //read only
    var name:String{
        get{
            return self.name
        }
    }
    
    //read/write Setters/Getters用来计算和获取属性,
    //并没有给成员变量实例分配存储,所以,会触发EXC_BAD_ACCESS.
    private var realAge:Int = 0;
    
    var age:Int{
        get{
            return realAge
        }
        
        set{
            realAge = newValue
        }
    }
    
    //read/write  with backing store,
    //类似 @property(nonatomic, assign)NSInter number;变量自动为_number
    private var _number:UInt = 0;
    
    var number :UInt{
        get{
            return self._number;
        }
        set(newNumber){
            if newNumber != number{
                self._number = newNumber
            }
        }
    }
    
    //read/write  with backing store,信息隐藏,监控属性变化,附加额外处理
     //类似 @property(nonatomic, retain)NSString address;变量自动为_address
    private var _address :String = ""{
        willSet{
    
            println("old Value = \(_address) will clear,new value = \(newValue) will set")
            
            self._address = newValue;
        }
        didSet{
            //可以追回一些额外的处理逻辑
             println("Property did set")
        }
    }
    
    var address :String{
        get{
            return self._address;
        }
        set(newAddress){
            if newAddress != _address{
                self._address = newAddress
            }
        }
    }
    
    
    init(){
        self.age = 0;
    }
    
}

2.调用

1
2
3
4
5
6
7
var baseClass:QBaseClass = QBaseClass()

//baseClass.name = "ksnowlv"//报错
baseClass.address = "北京海淀区"
baseClass.age = 20;
baseClass.number = 0917;
println("address =\(baseClass.address),age =\(baseClass.age),number =\(baseClass.number)")   

3.日志输出

1
2
3
old Value =  will clear,new value = 北京海淀区 will set
Property did set
address =北京海淀区,age =20,number =917

三.小结

objective-c中属性方法的不同之处:

1.生成属性方法实例的方式不同。

  • 1.objective-c中属性方法会自动为属性方法生成实例变量,为实例变量分配内存。

  • 2.而swift中属性方法仅仅是设置属性和获取属性,实例变量需要手动分配。

2.属性方法使用便捷性不同。

 objective-c的属性方法比较方便