Key-value coding allows for dynamic property getting and setting and the opportunity to support Cocoa bindings. See how key-value coding works in this video.
- Key value coding enables you to set properties in different ways, and it opens up the potential to add cocoa bindings. So lets look at how that works. I'm going to go to file, new, file, and I'm going to create a new cocoa class. I'll call this class book, and it will be a subclass of NSobject. And the reason why you want to subclass NSobject, is because it's the class from which we inherit those key value coding methods that we're going to be looking at right now.
Make sure the language is swift, then hit next. Save this in the default location, and there we go, the reason why it was inserted where it was, is because I had data selected here, and then it just added it as a child of data. So what we want to do is just move it inside of the data group, right here. Now of course, it's still going to work, in your Xcode project it actually doesn't matter where the files are really located on your screen, so long as they're in these groups. But usually when I create a cocoa project I want to have all my class files grouped together.
So here's my book, and what I'm going to do is just create a very simple definition, I'm just going to give it a few different properties here so, lets start with one called title, this is the title of the book, that'll be a string, and we'll give it a default value. Then we'll give it a property called author, and we'll give this a default value as well, and then finally an integer representing the number of pages. So we have some default data for our book object.
So save the file and head over to AppDelegate.swift, and we're going to look at instantiating a book. So in application to finish launching, I'm going to create a book right here. So define this we'll just call it book all lowercase. This is going to be of the type book, and then I'm going to call the book constructor. So now what we can do is we can actually just print out the author, so print out, book.author, run the application, and then look in the output window and we should see the default author that we created for our book class.
And there it is, person right there. So if you want to modify that property we can simply use .syntax, so right below the line where I define the book, I can change the author by typing book.author, and then, I'll be the author of this one. There we go. Now we can run the application, and sure enough we see the new value right in there. So I'll stop the application. Now I mentioned early in this movie that we're talking about key value coding, so we can modify properties through .syntax, but we can also use something called key value coding.
So we're going to comment out this line of code, and then go to the next line, and I'm going to change the author through key value coding. So that is, the name of the object that I want to modify, and then instead of modifying the property directly via .syntax, I'm going to call set value for key. Now the value is going to be a new string, lets say Jimmy is the author of this book, and the key is going to be a string, that is the name of the property.
So the name of the property that we want to modify is author. So we pass in the string of author for this key. So I run the application and I should see that Jimmy is the books author in the output window. And sure enough we see it there. Now the purpose of this is to be able to set properties dynamically. Again later on we're going to be able to use this with cocoa bindings to do some powerful things through interface builder. In other words, we can set values for properties through interface builder itself without having to write the code once we've created the appropriate objects and made the appropriate connections.
Finally what I want to show in this movie, is that we can actually access these values in the same way that we can set them, through key value coding. So instead of printing out book.author, I'm going to print out book.value for key, and then pass in the key. Again that's going to be the string, the name of the property which is author. So I run the application, and I should see the same result. And there it is, it's just an optional at this point because when you use key value coding it returns optional data.
So if you wanted to unwrap that we could always add the exclamation point there. So to review, we can modify properties dynamically using key value coding, using the set value for key method, and then we can access those properties using the value for key methods, and if you're wondering where those are coming from you can look up the documentation of the NSobject class.
- Installing your development tools
- Working with Xcode and the MVC pattern
- Creating basic interactions
- Creating delegate classes
- Building a user interface
- Laying out the UI
- Working with data and table views
- Binding data objects to the UI
- Debugging your app
- Distributing the app