Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
Created September 11, 2021 23:44
Show Gist options
  • Save RustyKnight/61e5da25c74b526257d121698d9d058d to your computer and use it in GitHub Desktop.
Save RustyKnight/61e5da25c74b526257d121698d9d058d to your computer and use it in GitHub Desktop.
Testing UserDefaults set(nil, forKey:) and removeObject(forKey:) to see size difference in the plist store
import UIKit
var greeting = "Hello, playground"
let key = "Test"
let value = "test".data(using: .utf8)!
let defaults = UserDefaults.standard
func printUD() {
print("UserDefaults after modification:\n")
defaults.dictionaryRepresentation().forEach { print("\($0): \($1)\n") }
print("-------------\n\n")
}
//defaults.set(value, forKey: key); printUD()
//defaults.set(nil, forKey: key); printUD()
//defaults.set(value, forKey: key); printUD()
defaults.removeObject(forKey: key); printUD()
// Use this to find the path of the plist
//var path: [AnyObject] = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true) as [AnyObject]
//let folder: String = path[0] as! String
//NSLog("Your NSUserDefaults are stored in this folder: %@/Preferences", folder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment