Filtering Dictionary in Swift 4 Fails in Xcode, But Succeeds in Playground

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



Leave a reply



Submit