What describes objective-C and Cocoa Bindings best?

前端 未结 2 985
小蘑菇
小蘑菇 2021-01-30 18:00

I have trouble understanding Cocoa Bindings. Can someone explain me what this is all about, in an way that is humanly perceivable?

2条回答
  •  时光说笑
    2021-01-30 18:52

    Previous answer is very comperhensive and good, I'd just thought I'd add an answer explains what it is at its core without involving Cocoa or Objective-C specifically. That is because the concept itself is language agnostic although dynamic languages like Objective-C makes it a lot easier than more static language like C++.

    Example

    Say you have two objects M and V. M has methods:

    setX(int x);
    setY(int y);
    int getX();
    int getY();
    

    While V has methods:

    setA(int x);
    setB(int y);
    int getA();
    int getB();
    

    One way of looking at this is that M has properties x and y and V has properties a and b. You want a change of property x to cause a change in property b and a change in y to cause a change in a.

    By change in property x we mean e.g.:

    M.setX(10)
    

    where previously

    M.getX() != 10
    

    So we want a call of setX on M to cause a call to setA on V.

    What bindings allow you to say is that property b on object V is bound to property x on object M. And then this updating is handled automatically. You as a coder don't have to write code that checks if x is changed and then call setB on V. Bindings takes care of this automatically.

    Summary

    Bindings allows you to bind two properties together that exist on two different objects, so that changing the value of one of the properties causes the dependant property in the other object to change to the same value.

提交回复
热议问题