Filtering dictionary in Swift 4 fails in Xcode, but succeeds in Playground
I tried to resolve this a few ways, including rebooting Xcode, clearing DerivedData
etc., but at the time nothing worked. I came back to the project several days later and found that the same code which had previously failed to build now built without issue (without my having made any relevant changes). So I'm blaming this on a quirk of the Xcode 9 beta. Or perhaps something was just gummed up somewhere and Xcode eventually cleared a cache or something of that nature. ¯\_(ツ)_/¯
Swift 4 Key Value Dictionary stored in User Defaults
There is a major mistake. You save a dictionary but retrieve an array.
Apart from that a dictionary retrieved from UserDefaults
is [String:Any]
by default, you have to conditional downcast the object.
The code checks if there is a dictionary in UserDefaults
and if there is the requested key in one expression
public func checkDatabaseMatch( _ name: String, _ number: String) -> Bool
{
guard let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String],
let databaseCheck = database[name] else { return false }
return databaseCheck == number
}
Another mistake is that you are always overwriting the entire dictionary in UserDefaults
. If you want to save multiple key-value pairs you have to read the dictionary first.
public func saveToDatabase( _ name: String, _ number: String)
{
var newEntry : [String: String]
if let database = UserDefaults.standard.dictionary(forKey: "Database") as? [String:String] {
newEntry = database
} else {
newEntry = [:]
}
newEntry[name] = number
UserDefaults.standard.set(newEntry, forKey: "Database")
}
Side note: The parameter labels are highly recommended in Swift for better readability.
What is causing items from UIPasteboard to be converted to NSConcreteMutableData?
The Data
you get for item[key]
is simply the UTF-8 encoded value for the string.
If you add the following case
, you will see that:
case let x as Data:
let str = String(data: x, encoding: .utf8)
print("str = \(str)")
The cause of the confusion is that the keys used by the items
property are UTIs, not random keys. If you change your key to public.text
then your code will work as expected.
Normally, you would not put a string on the pasteboard using the items
property. You would use the string
property to read and write the value.
UIPasteboard.general.string = "Hello"
let aStr = UIPasteboard.general.string
print("aStr = \(aStr)")
Doing this avoids the need to specify a UTI and it avoids replacing all existing items on the pasteboard.
ForEach on sorted dictionary: Xcode hangs on building with if block
You need view in ForEach content, so use Group, like
ForEach(testDict.sorted(by: >), id: \.key) { key, value in
Group {
if key != "b" {
Text(value)
}
}
}
iOS Swift Decodable: Error: Cannot invoke initializer for type with no arguments
Review has no default value , You need to change this
var profileValue = ProfileValue()
to
var profileValue:ProfileValue?
OR
var review: ReviewValues?
OR
supply init
method in ProfileValue
struct
Related Topics
How to Run App in Simulator Xcode 6
Convert Swift Encodable Class Typed as Any to Dictionary
Swift 3:Delegate Within Tapgesturerecognizer in Generics Doesn't Get Called
Code Only Retrieving One Value from Data in Firebase
Can the Byte Order of Double Be Safely Reversed
Compare Value from Two Struct in Swift
Swift:Program for Addition of 2 Numbers Using Closure
Swift 2 Unrecognized Selector in Tapgesture
Realm Data Doesn't Show Up on a Physical Device
How to Convert Unix Timestamp into Swift Nsdate Object
Swift Switch Char{ Case "\U{E2}:
Stopping Timer at Defined Amount of Time in Swift
Swift Why Isn't My Date Object That's (Equatable) Equal After Converting It to a String and Back
Downloading Firebase Storage Files Device Issue
Alamofireimage How to Clean Memory and Cache
Initializer for Conditional Binding Must Have Optional Type, Not '[String:Any]'
Handle Single Click and Double Click While Updating the View