What is Key-Value-Coding and Key-Value-Observing in Objective C?

前端 未结 4 490
情话喂你
情话喂你 2021-01-29 22:56

Can someone explain in simple terms what is Key-Value-Coding and Key-Value-Observing? Please don\'t provide links to Apple Developer\'s reference Docum

4条回答
  •  死守一世寂寞
    2021-01-29 23:35

    Key Value Coding is simply accessing a property of an object through a string instead of the literal syntax.

    // Here is a new instance of an object
    Foo *foo = [[Foo alloc] init];
    // Accessing a property called someValue with literal syntax:
    [foo someValue];
    // Accessing the same property with dot notation
    foo.someValue;
    // Accessing the same property with Key-Value coding:
    [foo valueForKey:@"someValue"];
    

    The power of KVC is that you can specify any arbitrary string at runtime (obviously this could be very dangerous too).

提交回复
热议问题