[swift-evolution] Custom didSet methods

cocoadev at charlessoft.com cocoadev at charlessoft.com
Tue Jan 12 15:26:56 CST 2016


Currently, didSet already gets called when a set property is mutated. 
The only difference I see in this proposal is that it lets you know more 
precisely what changed, so that you can more efficiently update the UI 
to reflect the change; i.e. if each item is represented by a view 
object, and you insert an object, the redraw only needs to occur for the 
view corresponding to the newly inserted item instead of having to 
recreate all the views.

Seems reasonable to me. KVO currently provides this facility in 
Objective-C.

Charles

On 2016-01-12 07:33, 肇鑫 via swift-evolution wrote:
> I don't know if this is a good idea. If you insert an item to an
> array, what should happen? The items after the inserted item should be
> considered as removed or not? They indexes are changed but the values
> not.
> 
> zhaoxin
> 
> On Tue, Jan 12, 2016 at 7:24 PM, James Campbell via swift-evolution
> <swift-evolution at swift.org> wrote:
> 
>> When declaring a collection, it would be nice to have a way of
>> knowing when an item is set/added and removed.
>> 
>> For example you could trigger a layer to be added to the layer
>> hiearchy.
>> 
>> var shapeLayers: [CAShapeLayer] {
>> 
>> didAdd(item) {
>> addSubLayer(item)
>> }
>> 
>> willRemove(item){
>> item.removeFromSuperlayer()
>> }
>> }
>> 
>> --
>> 
>>  Wizard
>> 
>> james at supmenow.com
>> +44 7523 279 698 [1]
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution [2]
> 
> --
> 
> Owen Zhao
> 
> 
> Links:
> ------
> [1] tel:%2B44%207523%20279%20698
> [2] https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list