swift-dictionary

How use use SwiftUI ForEach with a dictionary [ customEnum : customStrut ] - getting “conform to 'RandomAccessCollection'” error

纵然是瞬间 提交于 2020-12-23 11:59:35
问题 How could I correct this code from failing build? Basically wanting to use ForEach to iterate through a dictionary which is based on a [ customEnum : customStrut ]. Otherwise if this is problematic a different way to achieve that SwiftUI supports? Errors Referencing initializer 'init(_:id:content:)' on 'ForEach' requires that '[GCFilterViewOptions.FilterOptions : GCFilterViewOptions.FilterOptionValues]' conform to 'RandomAccessCollection' Type '(key: GCFilterViewOptions.FilterOptions, value:

Swift: dictionaries inside array

你离开我真会死。 提交于 2020-01-11 07:17:49
问题 Data: [ { firstName: "Foo", lastName: "Bar" }, { firstName: "John", lastName: "Doe" } ] How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest: var persons:Array = [Dictionary<String, String>()] but this gives me the error: Cannot convert the expressions type () to type Array<T> Any ideas? 回答1: The correct way is: var persons = [Dictionary<String, String>]() which is equivalent to: var persons = [[String :

Swift: dictionaries inside array

寵の児 提交于 2020-01-11 07:17:29
问题 Data: [ { firstName: "Foo", lastName: "Bar" }, { firstName: "John", lastName: "Doe" } ] How can I have this kind of structure using swift array and dictionary? This data shows dictionaries inside an array, right? So I suggest: var persons:Array = [Dictionary<String, String>()] but this gives me the error: Cannot convert the expressions type () to type Array<T> Any ideas? 回答1: The correct way is: var persons = [Dictionary<String, String>]() which is equivalent to: var persons = [[String :

How to write Dictionary to a file?

不打扰是莪最后的温柔 提交于 2019-12-21 03:15:41
问题 I have a FileHelper class where I've implemented 3 methods whose job is to write a Dictionary contents to a file. Those methods are: func storeDictionary(_ dictionary: Dictionary<String, String>, inFile fileName: String, atDirectory directory: String) -> Bool { let ext = "txt" let filePath = createFile(fileName, withExtension: ext, atDirectory: directory) /**** //If I use this method, file is created and dictionary is saved guard (dictionary as NSDictionary).write(to: filePath!, atomically:

Does the Swift standard Dictionary have a get-or-set function?

淺唱寂寞╮ 提交于 2019-12-12 13:11:51
问题 I found myself starting to want something like this: extension Dictionary { mutating func get(_ key: Key, backup: Value) -> Value { if let stored = self[key] { return stored } else { self[key] = backup return backup } } } but in my experience, Swift leaves out things like this because it has an alternative (intended) way to do it. I haven't found such a way in documentation. Did I miss this function, or should I create it? Also, if they left it out and I shouldn't create it, why? 回答1: No it

Extending Dictionary with key and value constraints

☆樱花仙子☆ 提交于 2019-12-10 11:02:27
问题 I want to extend dictionary and constrain it to specific key type of NSDate with values of type Array<MyClass> MyClass is a swift class with no subclass. extension Dictionary where Key: NSDate, Value: Array<MyClass>{ func myFunction() -> Int{ // Some thing useful } } The above code returns a build error: Type 'Value' constrained to non-protocol type 'Array' Ok so it seems that it needs to be constrained to a specific protocol, but strangely enough when I constrain Value to: extension

How to write Dictionary to a file?

我的未来我决定 提交于 2019-12-03 09:07:22
I have a FileHelper class where I've implemented 3 methods whose job is to write a Dictionary contents to a file. Those methods are: func storeDictionary(_ dictionary: Dictionary<String, String>, inFile fileName: String, atDirectory directory: String) -> Bool { let ext = "txt" let filePath = createFile(fileName, withExtension: ext, atDirectory: directory) /**** //If I use this method, file is created and dictionary is saved guard (dictionary as NSDictionary).write(to: filePath!, atomically: true) else { return false } */ guard NSKeyedArchiver.archiveRootObject(dictionary, toFile: (filePath?

Dictionary in Swift with Mutable Array as value is performing very slow? How to optimize or construct properly?

人走茶凉 提交于 2019-11-26 20:27:12
I am trying to build a data structure in Swift that maps an Integer to an array of objects (a dictionary with int as key and array as value). The objects are extremely small, and they simply wrap a UIColor and an Int. I have two implementations one that uses a Swift array as the Dictionary's value type, while the other uses a NSMutableArray as the value type. My objective-C code performs extremely fast, but my Swift code is running egregiously slow. Ideally, I would not like to use an NSMutableArray, and would like to keep it as a Swift array. Reason for this is I am writing an algorithm and

Dictionary in Swift with Mutable Array as value is performing very slow? How to optimize or construct properly?

那年仲夏 提交于 2019-11-26 12:17:28
问题 I am trying to build a data structure in Swift that maps an Integer to an array of objects (a dictionary with int as key and array as value). The objects are extremely small, and they simply wrap a UIColor and an Int. I have two implementations one that uses a Swift array as the Dictionary\'s value type, while the other uses a NSMutableArray as the value type. My objective-C code performs extremely fast, but my Swift code is running egregiously slow. Ideally, I would not like to use an