Questions tagged [swift-dictionary]

A mapping data structure in the Swift programming language, like Python dictionaries or Perl hashes.

Swift is a programming language designed for developing iOS and OSX applications. Dictionaries in the language are used to contain key-value pairs like dictionaries in Python or hashes in Perl. These types are known more generally as "associative arrays."

This tag should be used for questions about using dictionaries in Swift.

Relevant Links:

Swift collection types reference page in iOS developer library
Swift Overview Page
Associative Arrays Wikipedia page

68 questions
10
votes
2 answers

How to write Dictionary extension that handles optional values

I'm trying to implement a Dictionary extension and I want to handle optional values. But whatever I do, if I use my method on a [String: String?] dictionary, it fails to optionally bind the value. How do you write an extension to a dictionary that…
Rob
  • 415,655
  • 72
  • 787
  • 1,044
7
votes
2 answers

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

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…
AyBayBay
  • 1,726
  • 4
  • 18
  • 37
6
votes
3 answers

How to update swift dictionary value

I rewrite this code from php. And I find it difficult to make it work in swift. var arrayOfData = [AnyObject]() for index in 1...5 { var dict = [String: AnyObject]() dict["data"] = [1,2,3] dict["count"] = 0 …
Ben
  • 1,906
  • 10
  • 31
  • 47
5
votes
1 answer

How to properly decode nested JSON objects with Swift structs

Intent: Receive cryptocurrency price data via Coinmarketcap API, decode it into custom structs in SWIFT and potentially store that data in a database (either CoreData or SQLite). Context: I am receiving the following error on…
K307
  • 105
  • 7
5
votes
1 answer

How to interpret this Swift SpriteKit example code of a physics body bitmask system

I was taking a deep look into Apples SpriteKit & GameplayKit example code and found a Project called 'DemoBots' written in Swift. There are some very interesting concepts used in that projects which I wanted to adapt into my projects. I was already…
4
votes
2 answers

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

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…
Greg
  • 34,042
  • 79
  • 253
  • 454
4
votes
1 answer

Protocol Extension of Constrained Dictionary

I'm trying to get a specific Dictionary type to conform to a protocol. typealias FirebaseDictionary = Dictionary I would like to have the conform to a FirebaseValue protocol protocol FirebaseValue { // stuff here } I…
4
votes
3 answers

Optional AnyObject values in Swift dictionary

When I add a value of type 'AnyObject?' to a dictionary of Type '[String : AnyObject]' in the following way, the value can not by added, which is actually what I've expected. Assigning an optional type to a non-optional type should fail in my…
4
votes
1 answer

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

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 …
Ky -
  • 30,724
  • 51
  • 192
  • 308
4
votes
2 answers

Pass a swift closure to an objective-C function which takes a block as parameter

I have a function in objective-C as following - (void) fetchChannelListForWatch:(void (^)(NSDictionary *))callback I want to pass a swift callback closure into this like this: fetchChannelListForWatch(replyHandler) where replyHandler is a…
Jacky Wang
  • 618
  • 7
  • 27
4
votes
3 answers

Swift Sort Dictionary by property of an object value

I have a dictionary like and Loto is object like below; Loto: { "success": true, "data": { "oid": "64kbbqi8dbxygb00", "hafta": 961, "buyukIkramiyeKazananIl": "", "cekilisTarihi": "11/04/2015", …
Emre BEGEN
  • 577
  • 1
  • 7
  • 17
3
votes
2 answers

Swift : How do i create an array of dictionaries, where each dictionary contains an array inside them?

I am trying to load some data from my plist into an array. While declaring an array i did something like this var natureList : [Dictionary] = [] as! [Dictionary] I am getting this error Here is my plist file So how do i declare an array to load…
iPhoneDeveloper
  • 958
  • 1
  • 14
  • 23
3
votes
2 answers

How to write Dictionary to a file?

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, inFile fileName: String, atDirectory directory:…
nayem
  • 7,285
  • 1
  • 33
  • 51
3
votes
2 answers

Dictionary where Any may hold nil value in Swift 3

Can someone explain why this works in Swift 3? var dict: [AnyHashable: Any] let b: AnyObject? = nil let c = b as Any dict = ["a": "aa", "b": c] If I test dict["b"] == nil It returns false. Is it supposed to be right?
Rafael Sacchi
  • 33
  • 1
  • 1
  • 3
3
votes
1 answer

Cannot assign value of type '[[String : AnyObject]]' to type '[[String : AnyObject?]]'

I'm getting the following error: Cannot assign value of type '[[String : AnyObject]]' to type '[[String : AnyObject?]]' It's strange this assignment was working before then when I restarted my Xcode, I started to get this error. From what I have…
Ngoma Mbaku Davy
  • 169
  • 3
  • 11
1
2 3 4 5