-1

I am following this tutorial to get to know KVC. stated in that tutorial the following

[someObject.someProperty setText:@"This is a text"];

 Using KVC:

[self setValue:@"this is a text" 
forKeyPath:@"someObject.someProperty.text"]

my questions are:

1- when we used KVC, why did we use ".text" in forKeyPath?

2-does "forKeyPath" should always refer to the object we have or it can be just any text

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Do you understand what properties are and how they work? – Willeke Jun 23 '17 at 10:33
  • as far I researched, KVC is an approach by which one can access the properties of a class – Amrmsmb Jun 23 '17 at 10:35
  • Read this document clearly https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/index.html#//apple_ref/doc/uid/10000107-SW1 – selva raj Jun 23 '17 at 10:45
  • I didn't ask about KVC. To understand KVC you have to understand [properties](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW1). – Willeke Jun 23 '17 at 11:07
  • 1
    Recommended tutorial: [Back To Basics: Intro to Object Oriented Programming](http://www.appcoda.com/objective-c-object-oriented-programming-intro/) – Willeke Jun 23 '17 at 11:17

1 Answers1

1

The line of code from your tutorial:

[someObject.someProperty setText:@"This is a text"];

uses two different ways to access two different properties, as such it is a bit confusing.

Declaring a property, say windowHeight, declares two methods called the getter, which gets the property value, and the setter, which sets it. These methods are named propertyName and setPropertyName respectively, in our example windowHeight and setWindowHeight. Calling these methods is the original way in which properties where used. Your tutorial line written in this style is:

[[someObject someProperty] setText:@"This is a text"];

This calls the getter someProperty of someObject which returns another object, say someOtherObject, and then calls the setter setText of someOtherObject.

Later in the development of Objective-C dot notation was introduced as a shorthand for getters and setters. Using this notation the getter call [someObject someProperty] can be written as someObject.someProperty, while the setter call [someObject setSomeProperty:someValue] can be written as the assignment statement someObject.someProperty = someValue.

In any of the above dot expressions someObject is an expression which returns an object reference. It may be a simple variable reference; or it could be a more complex expression, including another dot expression, which returns an object reference.

Using dot notation your tutorial line is written:

someObject.someProperty.text = @"This is a text";

The parallels with the KVC keypath should now be apparent.

Your tutorial choose to mix'n'match original and dot notation styles in the same statement, probably resulting in your confusion and questions.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86