UserDefaults How can I get my own saved keys? - swift

I tried this code but it brings me all keys saved except me. How can I get my own saved keys?
print("UD: \(UserDefaults.standard.dictionaryRepresentation().keys) \n")
Console:
The key I saved is "Ağustos Test 1".
How can I get only this key?

You cannot "get" from user defaults just the keys for user defaults entries that you created in code. What's in your user defaults is what's in your user defaults; it doesn't have any way of distinguishing "who" created a particular entry.
Knowing the keys you added is your business. Typically this information is hard-coded into your app, e.g. you have a list of constants, usually as static properties of an enum or struct. If you are creating keys dynamically, then if you need to know the names of the keys you created, storing that information is entirely up to you.

IDEALLY you would know the key or group all your keys as a child of another key but thats not what you asked for :)
A few workarounds:
1. all:
you COULD enumerate all keys in userDefaults BUT you have to be aware you'll get keys, that aren't yours...
let dict = UserDefaults.standard.dictionaryRepresentation()
for key in dict.keys {
if let value = dict[key] {
print("\(key) = \(value)")
}
}
This will print all that is in there and that includes almost a hundred apple config values. so.. no good!
2. inclusive filter
if your keys have a commonality, you could invert the filter:
import Foundation
let included_prefixes = ["myprefs.", "myprefs2."]
//my keys
UserDefaults.standard.set(1, forKey: "myprefs.int1")
UserDefaults.standard.set("str1", forKey: "myprefs2.str1")
let dict = UserDefaults.standard.dictionaryRepresentation()
let keys = dict.keys.filter { key in
for prefix in included_prefixes {
if key.hasPrefix(prefix) {
return true
}
}
return false
}
for key in keys {
if let value = dict[key] {
print("\(key) = \(value)")
}
}
3. fragile exclusive filter
So if you really dont know your keys you COULD filter them out
import Foundation
let blacklisted_prefixes = ["Country", "NS", "com.apple", "com.omnigroup", "NavPanel", "WebAutomatic", "NSTableViewDefaultSizeMode", "sks_agent", "Apple", "PayloadUUID", "PKSecure", "_HI", "AK", "ContextMenu", "MultipleSession", "CSUI"]
//my keys
UserDefaults.standard.set(1, forKey: "int1")
UserDefaults.standard.set("str1", forKey: "str1")
let dict = UserDefaults.standard.dictionaryRepresentation()
let keys = dict.keys.filter { key in
for prefix in blacklisted_prefixes {
if key.hasPrefix(prefix) {
return false
}
}
return true
}
for key in keys {
if let value = dict[key] {
print("\(key) = \(value)")
}
}
BUT this is very fragile and not really advisable!
#needsmust

Related

How do I access the underlying key of a SymmetricKey in CryptoKit?

I am messing about with Apple's new CryptoKit framework on Xcode 11.0 beta 2. I want to create a SymmetricKey, then obtain the raw bytes of the key. I would like to use those bytes to create the same key, then check to make sure the keys are equal. From what I can understand in the documentation, the only way to get access to the raw bytes of a key is by using the withUnsafeBytes(_:) method. I have a Playground with the following code:
import Foundation
import CryptoKit
let key1 = SymmetricKey(size: .bits256)
key1.withUnsafeBytes { body in
let rawKeyBytes = body.load(as: Data.self)
let key2 = SymmetricKey(data: rawKeyBytes)
print("Are they equal? \(key1 == key2)")
}
The output of this is Are they equal? false, so unfortunately the keys do not match. Assuming I could get these keys to match, I'm also not sure how to convert rawKeyBytes into a string in order to view it in my Playground output. Overall I'm just not very familiar with UnsafeRawBufferPointer or ContiguousBytes.
There's no sense to compare one key to another like you said. If you want to extract the key, use this simple lines:
let keyb64 = key.withUnsafeBytes {
return Data(Array($0)).base64EncodedString()
}
Or remove base64EncodedString() only for Data to send to a server or put in the keychain.
Best regards
I had to do this same thing and ended up making some extensions to streamline the process:
import CryptoKit
import Foundation
extension SymmetricKey {
// MARK: Custom Initializers
/// Creates a `SymmetricKey` from a Base64-encoded `String`.
///
/// - Parameter base64EncodedString: The Base64-encoded string from which to generate the `SymmetricKey`.
init?(base64EncodedString: String) {
guard let data = Data(base64Encoded: base64EncodedString) else {
return nil
}
self.init(data: data)
}
// MARK: - Instance Methods
/// Serializes a `SymmetricKey` to a Base64-encoded `String`.
func serialize() -> String {
return self.withUnsafeBytes { body in
Data(body).base64EncodedString()
}
}
}
And tested this like so:
import CryptoKit
func symmetricKeyTest() {
let symmetricKey = SymmetricKey(size: .bits256)
let serializedSymmetricKey = symmetricKey.serialize()
guard let deserializedSymmetricKey = SymmetricKey(base64EncodedString: serializedSymmetricKey) else {
print("deserializedSymmetricKey was nil.")
return
}
print("Keys match: \(symmetricKey == deserializedSymmetricKey)")
}
I don't believe you can use load on Data quite that way. But in any case, what you want looks like this:
let key1 = SymmetricKey(size: .bits256)
let key2 = key1.withUnsafeBytes { SymmetricKey(data: Data($0)) }
print("Are they equal? \(key1 == key2)")
It is generally dangerous to access a value inside its own withUnsafeBytes block (for example, accessing key1 inside key1.withUnsafeBytes). I can't remember if it's forbidden in this specific case, but as a rule you should avoid it because it leads to exclusive-access violations.
The following seems to work for me
import CryptoKit
import Foundation
let key1 = SymmetricKey(size: .bits256)
key1.withUnsafeBytes { ptr in
let rawKeyBytes = Data(bytes: ptr.baseAddress!, count: ptr.count)
let key2 = SymmetricKey(data: rawKeyBytes)
print("Are they equal? \(key1 == key2)")
}

Swift guard else called on dictionary key with NULL value

If I have a Dictionary returned from a NSNotification containing the following
print(notificationObj.object)
Optional({
age = "<null>";
names = (
David
);
})
Then the guard else is called when trying to assign this to a variable:
guard let categories = notificationObj.object as? [String:[String]] else {
// Gets to here
return
}
How can I handle the case where a Dictionary key is null.
Your dictionary does contain ...
Optional({
age = "<null>";
names = (
David
);
})
... and ...
age = ... is String = String (value is single String),
names = ( ... ) is String = [String] (value is array of Strings).
You can't cast it to [String:[String]] because the first pair doesn't fit this type. This is the reason why your guard statement hits else.
Hard to answer your question. Dictionary contains names, you want categories, names key does contain David, which doesn't look like category, ... At least you know why guard hits else.
Your questions is not very clear.
However IF
You have a dictionary declared as follow [String:[String]]
And you want manage the scenario where a given key is not present
Like this
let devices : [String:[String]] = [
"Computers": ["iMac", "MacBook"],
"Phones": ["iPhone 6S", "iPhone 6S Plus"]
]
Then you can at least 2 solutions
1. conditional unwrapping
if let cars = devices["Car"] {
// you have an array of String containing cars here
} else {
print("Ops... no car found")
}
2. guard let
func foo() {
guard let cars = devices["Car"] else {
print("Ops... no car found")
return
}
// you have an array of String containing cars here...
cars.forEach { print($0) }
}
It appears that your printed notificationObject.object is constructed from a JSON string that looks like this:
"{ \"age\": null, \"names\":[\"David\"] }"
The reason that you are hitting your else clause is because age is actually a nil, and not a valid String array. I tried using [String: [String]?] and [String: NSArray?] neither of which seem to work. The type is actually an NSNull (which inherits from NSObject).
So you can cast to [String: AnyObject] and check for NSArray like this:
if let categories = j as? [String: AnyObject] where (categories["age"] is NSArray) {
print("age was array")
} else {
print("age is probably null")
}
You might be better off if your notification object simply omitted the "age" property when the value is null. Then you would be able to cast to [String: [String]].

accessing inner dictionaries (dictionary of dictionaries) with one entry, when not knowing what key or value for that entry are

This is how I declared my dictionary:
var dataDictionary: [NSIndexPath:[Int:Bool]]!
The inner dictionary will always have just one entry e.g. [1:true]. When I search dataDictionary I specify a NSIndexPath. The inner dictionary gets returned. Sth like this:
var innerDictionary = dataDictionary[indexPath] // is of type [Int:Bool]
I would like to access inner dictionarys key and value, but I don't want to specify an Integer or Bool, because it is unknown. I only want to get that Int and/or Bool value stored in inner dictionary. No search of inner dictionary is needed, because if will always contains just one entry. Maybe something like this:
var key = innerDictionary.key
var value = innerDictionary.value
How can this be accomplished?
If your dictionary has only one entry, and you don't know the key, with Swift 2 you can get the first element of the keys sequence safely with if let:
let innerDictionary = [42: true]
if let k = innerDictionary.keys.first {
print(k) // prints 42
} else {
// dict is empty
}
Same for the value:
if let v = innerDictionary.values.first {
print(v) // prints true
} else {
// key has no value
}
Try by this way:
func nestedDic(){
var innerDictionary:NSDictionary = ["Item 1": "data 0", "Item 2": "data 1"]
//NSLog("original object:\(innerDictionary)")
NSLog("all keys array:\(innerDictionary.allKeys)")
if(innerDictionary.isKindOfClass(NSDictionary)){
for key in innerDictionary.allKeys{
let value = innerDictionary.valueForKey(key as! NSString as String)
NSLog("key value = \(value as! String)")
}
}
}
You could create a simple struct for the inner dictionary if there is only one entry, then you are able to use your favorite property names.
struct InnerDictionary {
var key = 0
var value = false
}
let indexPath = NSIndexPath(index: 1)
var dataDictionary = [indexPath: InnerDictionary(key: 1, value: true)]
if let innerDictionary = dataDictionary[indexPath] {
var key = innerDictionary.key
var value = innerDictionary.value
}

Saving more than one value to a Dictionary key in a loop with an Array

I have this block of code
//start of the loop
if let objects = objects as? [PFObject] {
for object in objects {
//saving the object
self.likerNames.setObject(object["fromUserName"]!, forKey: saveStatusId!)
}
}
likerNames is an NSMutableArray declared earlier, saveStatusId is a string I also declared and saved earlier (It's just an objectId as a String), and object["fromUserName"] is an object returned from my query (not shown above).
Everything is working fine as it is but my query sometimes returns more than one object["fromUserName"] to the same key which is saveStatusId. When this happens the value I have for that saveStatusId is replaced when I actually want it to be added to the key.
So want it to kind of look like this
("jKd98jDF" : {"Joe", "John"})
("ksd6fsFs" : {"Sarah"})
("payqw324" : {"Chris", "Sarah", "John"})
I know you can use Arrays but I'm not sure how I would go about that to get it to work in my current situation.
So my question would be how to I get my key (saveStatusId) to store more than one value of object["fromUserName"]?
Something like this could work
let key = saveStatusId!
let oldValue = self.likerNames.objectForKey( key ) as? [String]
let newValue = (oldValue ?? []) + [ object["fromUserName" ] ]
self.likerNames.setObject( newValue, forKey: key )
If likerNames has an array in slot[saveStatusId], append the new value, otherwise create an array and put that in the right slot

Determining if Swift dictionary contains key and obtaining any of its values

I am currently using the following (clumsy) pieces of code for determining if a (non-empty) Swift dictionary contains a given key and for obtaining one (any) value from the same dictionary.
How can one put this more elegantly in Swift?
// excerpt from method that determines if dict contains key
if let _ = dict[key] {
return true
}
else {
return false
}
// excerpt from method that obtains first value from dict
for (_, value) in dict {
return value
}
You don't need any special code to do this, because it is what a dictionary already does. When you fetch dict[key] you know whether the dictionary contains the key, because the Optional that you get back is not nil (and it contains the value).
So, if you just want to answer the question whether the dictionary contains the key, ask:
let keyExists = dict[key] != nil
If you want the value and you know the dictionary contains the key, say:
let val = dict[key]!
But if, as usually happens, you don't know it contains the key - you want to fetch it and use it, but only if it exists - then use something like if let:
if let val = dict[key] {
// now val is not nil and the Optional has been unwrapped, so use it
}
Why not simply check for dict.keys.contains(key)?
Checking for dict[key] != nil will not work in cases where the value is nil.
As with a dictionary [String: String?] for example.
The accepted answer let keyExists = dict[key] != nil will not work if the Dictionary contains the key but has a value of nil.
If you want to be sure the Dictionary does not contain the key at all use this (tested in Swift 4).
if dict.keys.contains(key) {
// contains key
} else {
// does not contain key
}
Looks like you got what you need from #matt, but if you want a quick way to get a value for a key, or just the first value if that key doesn’t exist:
extension Dictionary {
func keyedOrFirstValue(key: Key) -> Value? {
// if key not found, replace the nil with
// the first element of the values collection
return self[key] ?? first(self.values)
// note, this is still an optional (because the
// dictionary could be empty)
}
}
let d = ["one":"red", "two":"blue"]
d.keyedOrFirstValue("one") // {Some "red"}
d.keyedOrFirstValue("two") // {Some "blue"}
d.keyedOrFirstValue("three") // {Some "red”}
Note, no guarantees what you'll actually get as the first value, it just happens in this case to return “red”.
My solution for a cache implementation that stores optional NSAttributedString:
public static var attributedMessageTextCache = [String: NSAttributedString?]()
if attributedMessageTextCache.index(forKey: "key") != nil
{
if let attributedMessageText = TextChatCache.attributedMessageTextCache["key"]
{
return attributedMessageText
}
return nil
}
TextChatCache.attributedMessageTextCache["key"] = .some(.none)
return nil
If you want to return the value of the key you can use this extension
extension Dictionary {
func containsKey(_ key: Key) -> Value? {
if let index = index(forKey: key){
return self.values[index]
}
return nil
}
}
if dictionayTemp["quantity"] != nil
{
//write your code
}
If you are dealing with dictionary that may contain nil value for a key then you can check existence of key by:
dictionay.index(forKey: item.key) != nil
For getting first value in dictionary:
dictionay.first?.value // optional since dictionary might be empty