First of all, I would like you to recognize that UserDefaults
is a framework designed mainly to store a small amount of user-specific settings, such as the app's configuration screen.If the array has more than thousands of elements, you should avoid using UserDefaults
.
(Unfortunately, due to its ease of use, UserDefaults
tends to be used a lot in introductory articles for beginners, and even professionals who have some experience post apps in the App Store that are made in a proper way...)It may affect the overall performance of the app, so if you are a beginner in app development, please try to adopt the appropriate method.)
First, review the data types that can be stored in UserDefaults
.
A default object must be a property list —that is, an instance of (or for collections, a combination of instances of) NSData
,
NSString
, NSNumber
, NSDate
, NSArray
, or NSDictionary
.If you
want to store any other type of object, you should type archive
It to create an instance of NSData
.
(Saving)
The object for UserDefaults
must be a property list.It means NSData
, NSString
, NSNumber
, NSDate
, NSArray
, or
One of the NSDictionary
instances (or, for collection type, a combination of instances).If you want to save other types of objects, it is common to archive them and create an instance of NSData
.
For Swift, Data
, String
, built-in numeric and Bool
, Date
, Array
, Dictionary
(Array
and Dictionary
are all key values).CLLocation
is none of these (not plist compatible) and cannot be saved in UserDefaults
as it is.
When storing such data, you can use the following methods:
Archive (as described in the UserDefaults
document), convert to Data
, and register with UserDefaults
.
Save directly to a file.(The contents of the file are in byte columns, so "archive and convert to Data
" above is also used in this case.)
Use Core Data.
Use third-party libraries such as Realm and SQLite directly.
Therefore, the 3. and 4. methods are too exaggerated to store a simple array, but they are less beneficial, so I will explain how to do 1. (which can also be applied to 2.) below.
Currently, Swift has two mainstream archiving methods (serialization in other languages, sometimes referred to as "serialization"): using the Codable
protocol (new, hard to use unless you are a native Swift type) and using the NSObject
protocol since the Objective-C era.(If you look for it, there are other things.)
The CLLocation is simpler because it implements the NSSecureCoding
protocol that inherits NSCoding
.
From CLLocation
to create Data
and save it to UserDefaults
.
var locations: CLLocation = [ ]
// put a value in `locations`
//...
let locationData = NSKeyedArchiver.archivedData(withRootObject:locations)
UserDefaults.standard.set(locationData, forKey: "locationData")
One line to convert to Data
and one line to UserDefaults
.
(It's not that difficult to save to a file instead of UserDefaults
).
Code to retrieve Data
from UserDefaults
and restore it to its original CLLocation
.
if
let locationData = UserDefaults.standard.data (forKey: "locationData"),
let locations = NSKeyedUnarchiver.unarchiveObject(with:locationData) as ?[CLLocation]
{
print(locations)
Processing with // `locations`
//...
} else{
print("NO locationData in UserDefaults")
// What to do when UserDefaults do not have locationData
//...
}
It's a little longer when we use if-let to determine if the data exists in UserDefaults
and if it can be converted to [CLLocation]
, but it's actually one line each.
The explanation may have been a little persistent and difficult to read, but try each code, incorporating it into your project, etc.
© 2024 OneMinuteCode. All rights reserved.