The intention behind NSUserDefaults is to save contextual data relevant to the application state, for example saving the user's preferences, the state of the application when the user stopped using it (so you can return to that state when it fires up), login session data, etc..
Core Data is a more appropriate way to store persistent data, you can map your data model as you like and has a broader variety of options to save datatypes.
Whilst NSUserDefaults is available "everywhere", this should not be a turning point to decide if this is a better option for saving your data.
You can write a singleton class that serves as a data provider so that you can access your data in the same way you access the NSUserDefaults shared instance. You just need to keep in mind that this class or module should do only one thing, serve as an interface between your model and your implementation, so you do not store any objects in this class, just use it to pass over the requests to get and save data to CoreData.
This class could look something like this:
class CoreDataProvider {
static let sharedInstance = SUProvider()
let managedObjectContext : NSManagedObjectContext
let sortDescriptor: NSSortDescriptor
let fetchRequest: NSFetchRequest
private init(){
managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
fetchRequest = NSFetchRequest(entityName: "MyObject")
sortDescriptor = NSSortDescriptor (key:"objectAttribute", ascending:true)
self.fetchRequest.sortDescriptors = [self.sortDescriptor]
}
func getSavedObjects() -> [MyObject]? {
fetchRequest.sortDescriptors = [sortDescriptor]
do {
return try self.managedObjectContext.executeFetchRequest(fetchRequest) as? [MyObject]
} catch {
print("no records found")
}
}
}
Which you would use like this:
func getAllRecords() {
let records = CoreDataProvider.sharedInstance.getSavedObjects()
//- Do whatever you need to do
}