Download apps/ios/Tests/TestDefaultsSupport.swift from Darochin/openskynet: direct link, hf CLI and curl.
- Browser
- Download file 715 Bytes
-
https://huggingface.co/Darochin/openskynet/resolve/main/apps/ios/Tests/TestDefaultsSupport.swift
- Command line
-
hf download hf://Darochin/openskynet/apps/ios/Tests/TestDefaultsSupport.swift
-
curl -L -o TestDefaultsSupport.swift https://huggingface.co/Darochin/openskynet/resolve/main/apps/ios/Tests/TestDefaultsSupport.swift
715 Bytes
| import Foundation | |
| func withUserDefaults<T>(_ updates: [String: Any?], _ body: () throws -> T) rethrows -> T { | |
| let defaults = UserDefaults.standard | |
| var snapshot: [String: Any?] = [:] | |
| for key in updates.keys { | |
| snapshot[key] = defaults.object(forKey: key) | |
| } | |
| for (key, value) in updates { | |
| if let value { | |
| defaults.set(value, forKey: key) | |
| } else { | |
| defaults.removeObject(forKey: key) | |
| } | |
| } | |
| defer { | |
| for (key, value) in snapshot { | |
| if let value { | |
| defaults.set(value, forKey: key) | |
| } else { | |
| defaults.removeObject(forKey: key) | |
| } | |
| } | |
| } | |
| return try body() | |
| } | |