Structures
The following structures are available globally.
-
Wrapped Date variables which constrain their values to the start of the enclosing interval
Usage example:
func demo_usingPropertyWrappers(_ date: Date) { @WholeHour var ymdHour = date @WholeHours var ymdHours = [date, date.addingTimeInterval(100000)] @WholeDay var ymDay = date @WholeMonth var yMonth = date print("date:", date.EEEE_ddMMyyyy_HHmmss) print("ymdHour:", ymdHour.EEEE_ddMMyyyy_HHmmss) print("ymdHours:", ymdHours.map {$0.EEEE_ddMMyyyy_HHmmss}) print("ymDay:", ymDay.EEEE_ddMMyyyy_HHmmss) print("yMonth:", yMonth.EEEE_ddMMyyyy_HHmmss) } let date = Date(timeIntervalSinceReferenceDate: 600000083) demo_usingPropertyWrappers(date)
Printed output:
See moredate: Monday 06.01.2020 11:41:23 ymdHour: Monday 06.01.2020 11:00:00 ymdHours: ["Monday 06.01.2020 11:00:00", "Tuesday 07.01.2020 15:00:00"] ymDay: Monday 06.01.2020 00:00:00 yMonth: Wednesday 01.01.2020 00:00:00
Declaration
Swift
@propertyWrapper public struct WholeMonth
-
Usage example: see WholeMonth
See moreDeclaration
Swift
@propertyWrapper public struct WholeDay
-
Usage example: see WholeMonth
See moreDeclaration
Swift
@propertyWrapper public struct WholeHour
-
Usage example: see WholeMonth
See moreDeclaration
Swift
@propertyWrapper public struct WholeHours
-
Encapsulates a
[String:T]
dictionary whose values are automatically persisted (each in its own file).Exposes methods and properties similar to those of the Swift Dictionary.
Usage examples:
Create a
FileBackedDictionary
instance, using Documents subdirectory namedResources
andstruct Resource
as the value type.let directoryName = "Resources" var fbDict = FileBackedDictionary<Resource>(directoryName: directoryName)
Add resource instances
fbDict["apples"] = Resource(name: "apples", value: 1, quantity: 1) fbDict["oranges"] = Resource(name: "oranges", value: 2, quantity: 2) fbDict["mangos"] = Resource(name: "mangos", value: 3, quantity: 3)
Look up the resource info
let count = bDict.count // 3 let keys = fbDict.keys // ["apples", "oranges", "mangos"]) let values = fbDict.values // ... let myOranges = fbDict["oranges"]
Remove resources
fbDict["oranges"] = nil fbDict.removeAll()
At its initialization (typically at the application start), an instance of
See moreFileBackedDictionary
recovers the keys and values from the file storage.Declaration
Swift
public struct FileBackedDictionary<T> where T : Decodable, T : Encodable
-
Usage examples:
Sample usage:
func demo_TimerDeltaSigma() { var tds = TimerDeltaSigma() for _ in 0 ..< 10 { tds.printElapsedTimes() } print() for _ in 0 ..< 5 { tds.printElapsedTimes(.ms) } print() for _ in 0 ..< 5 { tds.printElapsedTimes(.s) } print() }
Sample output:
See moreΔ 0.000000, Σ 0.000000 s Δ 0.000082, Σ 0.000082 s ... Δ 0.000022, Σ 0.000260 s Δ 0.000021, Σ 0.000281 s Δ 0.000, Σ 0.000 s ... Δ 0.000, Σ 0.000 s Δ 0, Σ 0 s ... Δ 0, Σ 0 s
Declaration
Swift
public struct TimerDeltaSigma
-
Usage notes: CodableUserDefault
vs. PlistUserDefault PlistUserDefault supports plist-compatible types: Data, String, Number, Date, Array and Dictionary.
CodableUserDefault supports any type conforming to the Codable protocol (which includes plist-compatible types, but also structs and enums conforming to Codable.
Note: PlistUserDefault must be used for settings declared in Settings.bundle/Root.plist (because these are stored in type-specific formats, while CodableUserDefault stores/retrieves all types as Data).
See moreDeclaration
Swift
@propertyWrapper public struct CodableUserDefault<Key, Value> where Key : RawRepresentable, Value : Decodable, Value : Encodable, Key.RawValue == String
-
Wrapper PlistUserDefault
See moreenables creation of variables of any .plist-compatible type (Data, String, Number, Date, Array and Dictionary) Declaration
Swift
@propertyWrapper public struct PlistUserDefault<Key, Value> where Key : RawRepresentable, Key.RawValue == String