|
I found the article about dynamiclookup, but I'm really struggling moving from Python to Swift when it comes to dictionaries. I've been posting questions over at stackoverflow, but I figured I would ask here as well. In Python I can have a dictionary, we'll call 'calDict', and the structure looks like this:
In Python one creates the dictionary by declaring as
I could then create 2020
Then you'd create the months:
Then you'd create the days:
If events exist for the day, they're further created:
How would I implement this in Swift? I'm using XCode 11.5 with Swift 5.2 on macOS Catalina 10.15.5. I have tried in Swift Playground:
But all this does is make Swift Playground complain a lot. I'm just not able to wrap my brain around how to do this. Thanks for any help! |
|
That seems like a terrible structure to try to use. And not very Swifty at all. Things like this will give you trouble because you can't story heterogeneous items in an array or dictionary:
You could have a dictionary keyed by an Here's a quick and dirty (very quick and very dirty) attempt to replicate it in a more Swift-like fashion.
Be aware, however, that there is a lot that can go wrong here. For instance, there is no restriction on the I'd really think about some other way of approaching this data rather than trying to replicate what you are doing in Python. |
|
@roosterboy, thank you for the explaination. I've been told to read up on structures and I have, but I did not see a way to keep the data heterogenous by what I was reading because the examples were not very clear to me. I use a very large dictionary to hold all my application data. It's a music type application that reads in playlists and I hve a lot of properties I keep for each playlist and other items. The dictionary in Python was extremely helpful, easy and fluid for me to use. Swift's struct that you showed me above has lead me into a new direction, so I really appreciate your example. I see the issue you mention about January 42nd, May -3rd and year 3287. I will think about a way to programmitcally to limit the month days to positive integers within the limits for that month as well as the year within a reasonable time frame (i.e. 50 years?). |
|
I just wanted to let you know @roosterboy that I finally got my dictionary structure figured out. Thanks a million! I can now move forward with the conversion of my software. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.