Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Singleton Examples in Cocoa3:07 with Bjorn Chambless
Review some singleton examples from Apple's Cocoa Frameworks.
- Singletons by Matt Galloway
- UIDevice on iOS Dev Diary
- Factory Methods
- Lazy Instantiation
- XCode Project Templates
- Static Keyword in C
- Concurrency Programming
- instancetype on NSHipster
- Mac File System Programming Guide
- Grand Central Dispatch In Depth
- iOS Unit Testing
- Dependency Injection
Now we'll look at some examples of singletons from Apple's Coco Frameworks.
NS User Defaults is a simple mechanism for data persistence.
It provides a dictionary interface for
storing small amounts of app specific data.
Typically these are things such as configuration preferences, high scores in
the case of a game, or a date stamp of the last execution of the app.
NSUserDefault reads from and writes to the device's file system,
but hides the complexity associated with file io.
NSUserDefaults offers the statdardUserDefaults class method for
retrieving the singleton instance.
Storing values is done through set object for key.
Or the specific method for simple types.
Retrieving values is done with object for key.
[SOUND] This code snippet shows using set object for
key to save an NS number, 342, with the high score key and
the ns user defaults persistent store.
The call to synchronize forces the record to be written immediately.
It is usually safe to leave this out as ns user defaults data
is written out periodically and also prior to application exit.
The call to objectForKey can be used to retrieve the stored
high score value which we would expect to be 342.
NS file manager is the primary interface for file system interaction in coco.
It provides functionality for file system reading, writing, directory traversing,
creating paths and interacting with iCloud ubiquity containers.
Though it exposes more file system details than NS user defaults,
it still abstracts away much of the Unix file system complexity.
Given an NS string path for some file system location,
dirPath, this code snippet will test for the existence of a file,
somefile.txt, as is typical and NS file
manager is used as a singleton through the default manager factory method.
The NSFileManager singleton makes sense since there's only one file system for
the application to interact with.
A ubiquitous singleton in iOS,
as one is present in every running iOS app, is UIApplication.
UIApplication represents the app in the interface between the operating system and
a running application.
The instance reference is available through the shared application
However most interactions occur through the UI application delegate protocol.
These methods are some of the first user code to execute in an application.
Specifically, willFinishLaunchingWithOptions and
applicationWillTerminate, and applicationDidReceiveMemoryWarning
are all critical for reacting to important events in the life cycle for your app.
You need to sign up for Treehouse in order to download course files.Sign up