Storing and retrieving data

- Let's take a look at using NSUserDefaults…in our basic Calendar app to add persistence.…First, I'm going to add a new Swift file.…It will be a class called CalendarEvent.…It will have two Properties.…One for the title of the event,…and another for the date the event will take place.…Then, I'll make an initializer for it.…

So, init(withTitle)…which is a String,…andDateString…which is also a String, and then…I can set initial values for those Properties.…I'll want to store the CalendarEvent Object…in NSUserDefaults, so I'm going to implement…a couple of methods from NSCoding,…so that it can be stored correctly.…In order to do that, I'm going to make CalendarEvent…a subclass of NSObject.…

Now, I can implement the NSCoding methods.…The first that I want to implement is…encodeWithCoder.…This method will be called…when it's time to put this CalendarEvent into…the NSUserDefaults key value store.…So, I'll write,…coder.encodeObject…dateString…forKey…and we'll use the same name, dateString.…

And then, I'll do the same thing…with the title.…

Resume Transcript Auto-Scroll

Author

Released

2/3/2016

Learn Swift 2. These tutorials take you through the basics of Swift, the fast, interactive codebase for programming iOS, Mac OS X, tvOS, and even Watch OS apps. Swift is ideal for new programmers who want to build their first apps, as well as experienced developers who are migrating from Objective-C.

First, learn how to install Xcode, the IDE designed for Apple developers, and use Swift playgrounds, an innovative coding environment that displays results as you code. Author John Nastos then dives deep into Swift syntax: the variables and constants, data types, arrays, loops, classes, and functions that make your apps run. Next, discover how to build an engaging app interface with storyboards and Interface Builder, connect the interface and code, and use table views to present lists. John shows how to store and retrieve data, and interact with Objective-C, the original app development language that still integrates beautifully with Swift. Using the exercise files provided with the course, you'll assemble a complete working app with Swift while gaining the skills to go create the next one on your own.