Home
Username:
Password:
Objective-C and Xcode Essential Training Tutorials

Continuing Examples with Interface Builder / NSUserDefaults pt. 5




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

OK, back in Xcode we are now going to write the code to save our defaults and I think for this particular application the best place to put it, instead of providing a button or some mechanism that the user has to click on, is just to save off the defaults when the application quits and the NSApplication Class makes this very easy to do because we get notified before our application is going to terminate and that Method is called applicationWillTerminate and there it is. And the code that we're going to put inside of here, we're going to say self and then we're going to give it a Method name. Now the Method we haven't written yet, but we will in just a second, but I want to demonstrate how you call Methods within your Class file and this is how you do that, you use self. Self is where you are right now in the application, so when the application's running it will know that it's inside this Class file and so it'll look for that Method inside of here and then when it finds it it'll execute it. So our Method is going to be called updateUserDefaults. Seems appropriate enough. So I'm going to copy that and I'm going to come up just before awakeFromNib and Objective-C, just like C, reads the Class file from top to bottom, so as long as I put Methods above where it's being called, the Compiler would have already found it and we won't get Compiler warnings. Now there's other ways around this, too, but for right now this is the simplest way to do this. So in this Method we're going to need to do the same thing. We're going to need to get an Instance of NSUserDefaults. I think instead of typing this I'm just going to come down and highlight this code here, copy that and paste it. Now that we have an Instance we're going to say userDefaults and this time we're going to say setObject forKey. So this is the Method that we're going to use. We're going to say set the object and the object that we want to get is the textView and its string value. And if you remember, the Method for that is just string and then the key we've already determined when we first established our application in the Initialize Method and that's urls and I'll close the bracket off and a semicolon and to ensure that this gets written to file we are going to say userDefaults synchronize. And that's it. That's all there is to saving our userDefaults as soon as our application is about to quit. So we can run this and I'll go ahead and add in a URL. I'll do apple.com. I'll close it and then I'll run it again and when I do our defaults were read from our plist file and inserted into our Text View. At this point, why don't we close this and we'll hide Xcode and why don't we go to the Finder and take you over there and show you what that looks like. So, I'm at my own folder, I'm going to go to Library, going to scroll down to Preferences and right here at the top we have com AllenCraig DefaultsExample plist. Now I can get to this real easy just by using the Spacebar and we can get a quick look at it and we can see that we have a dictionary with a key value of urls and our string with the three urls that we have, it ends the string, ends the dictionary and ends the plist. NSUserDefaults is a powerful tool provided by Apple. You'll use it in nearly every application that you create.

Tutorial Information

Course: Objective-C and Xcode Essential Training
Author: Craig Williams
SKU: 34088
ISBN: 1-936334-00-3
Release Date: 2010-02-22
Duration: 6 hrs / 68 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available