Apple Key Value Coding - can someone explain to a C # developer why I need it and what it does?

Before switching to iOS development through Monotouch, I played a little with ObjectiveC. Since then, in my opinion, "Key Value Coding" (KVC). I never understood what it was good for and what its magic was.

In my opinion, these are just collections of names and values, such as .NET NameValueCollection : instead of setting anObject.Address = "An address Value" you should write anObject.Properties["Address"] = "An address value" . Fine. So what? Since this seems to me too easy, I am sure that this cannot be.

Or is it because .NET is reflected and therefore does not need something like Key-Value-Coding? I can capture the type β€œ anObject ” and use reflection to encode all its properties and look for one called β€œAddress”, and then use reflection to set the value. Perhaps this is an analogy of KVC?

I hope there is someone who is pro in ObjC and C # /. NET, who can explain to me what benefits and how it will translate to .NET. Do not give me only Apple documentation. I know all this. I'm trying to find out if there has been anything in my coding life so far without knowing KVC? Or could I have used something similar in .NET without realizing that it translates to KVC if I use ObjC?

+7
source share
3 answers

From someone who uses both money, probably already 3 years:

As you understand, there is nothing surprisingly complicated in KVC that cannot be done with dictionaries and NameValueCollections in C #.

The big difference is that KVC is built on the language. An object in Obj-C IS a NameValueCollection. You do not need to change the development method (from classes and properties to dictionaries) to use it. You can create your object with the properties you want, and then later call valueForKey: @ "name", and it still works.

Now you can say: "Yes, cool, but I can do it with reflection!". Of course, you can, but, as before, you will have to change the way you develop, apart from the fact that reflection in general is much slower.

Another interesting feature of KVC is that it allows you to use KVO, which basically allows you to register to receive notifications of changes in any object without having to write one line of code in these objects. Thus, in any object of your application, you can call "addObserver: forKeyPath:" and get a callback if any of your application changes this property. This is really useful for live apps like iOS apps.

Again, you can implement KVO in C # (MVVM frameworks, for example, all this time), but for this you need to write additional code. In Obj-C, it is built on the language.

+11
source

I report this in the context of MonoMac, the MonoTouch peer-to-peer project, but used to create Mac applications:

http://tirania.org/monomac/archive/2010/Dec-07.html

Key-Value Coding is a set of methods that allow applications to access object properties using strings. This is similar to Binding Expressions in Silverlight. In both cases, the goal is to allow tools that do not directly have access to your own code to access properties from your program.

In particular, this is useful because some APIs can take advantage of this. For example, CoreAnimation can animate properties, given their "path" to the object. For example, you can:

 var animateX = CAKeyFrameAnimation.FromKeyPath ("position.x"); pos.Values = new NSNumber [] { 0, 10, 60 }; layer.AddAnimation (animateX, "move"); 

"position.x" in this case refers to the position of the layer, and inside this position - the component X.

The blog above describes in more detail how you can actually open your own objects to participate in this protocol (by registering your own properties to make them visible to the Key-Value-Coding system).

Kenneth, another of the MonoMac developers, spoke about this in detail:

http://cocoa-mono.org/archives/153/kvc-kvo-and-cocoa-bindings-oh-my-part-1/

In particular, it looks at the similarities to Reflection, and it shows you how to use [Export] to turn your C # code into KVC compatible code.

+7
source

If you access this property

 anObject.Address = "An address Value" 

The code will be very "static". He will always have access to Address

You can create more dynamic code like this

 void SetProperty (string propertyName, string value) { anObject.Properties[propertyName] = value; } 

You would do this if you did not know at compile time which property would be available.

In C #, you usually use Dictionary<TKey,TValue> to store key / value pairs. Automatic access to properties through KVC, as in Objective-C, is not supported in C #. You either declare the property as

 public Dictionary<string,string> Properties { get; private set; } 

and create an instance in the constructor of the class with

 Properties = new Dictionary<string,string>(); 

then you can access it as follows

 anObject.Properties[propertyName] = value; 

Or you will need to use Reflection to access the property

 Type type = anObject.GetType(); // Or Type type = typeof(TypeOfAnObject); PropertyInfo prop = type.GetProperty(propertyName); prop.SetValue(anObject, propertyValue, null); 

However, this is not very effective.

+4
source

All Articles