|
I need to create and maintain a list of ‘used numbers’ every year which are also uniquely associated with a colour. I’m thinking I will need to store this in its own CoreData entity, but I’m trying to model the entity using structs so that I can work out the interface. I’ve been doing this over a large app already. I would like help knowing should this be an array? A set? And the intricacies of getting an NSSet to an array from CoreData, plus I get muddled up on Getters and Setters in Swift, when they are appropriate. I also struggle with optional Dates and presenting them to a user. So far I have this as my struct:
the enum:
The numbers must be unique within colour and year. When the user needs an unused number for a given colour and year then the app must offer the lowest unused number that is available for them to use. So it can be saved in the used number list. Sometimes the user might accidentally use a much larger number than the lowest available and they must add this number to the used number list. For example, Orange 78 might be the next highest number, but the user might accidentally need to record that they used Orange 87. So the Orange sequence available will subsequently be Orange - 78, 79, 80, 81, 82, 83, 84, 85, 86, 88… etc. The interface looks like this so far - The user picks their spray colour. Next they should be presented with this list of unused numbers. https://drive.google.com/file/d/1B1bOPjHFNJXVpCqUBJid-NEFNflJNPMa/view?raw=1 Happy to include any other code if it helps. Thanks, Vickie |
|
Note: If you use Dropbox, you can change the Example: This link:
results in this image: |
|
Thanks so much, @roosterboy. Do you think the list of unused numbers should be a set or an array of ints? or? Thanks, Vickie |
|
Definitely a set to store the list of used numbers. And from there it would be easy enough to determine what the next available number should be, even with gaps in the sequence. When I'm back at my computer later, I can post some example code if you need some pointing in the right direction. |
|
|
|
Whilst waiting for @roosterboy's code, here is some code that you can put into the playground and see the effect.
|
|
Without knowing exactly how these are being used in your code (e.g., in a picker, in a freeform text field, etc), it's hard to be more specific in my example, but the general principle you would follow can be illustrated as below:
Make sense? |
|
Thank you both so much. Just for info, I thought I'd present finished code before marking this as solved. However, as @roosterboy said, "Without knowing exactly how these are being used in the code (e.g., in a picker, in a freeform text field, etc)" is now the reason it's taking me longer to answer. I'll give it one more day then just mark as solved. Maybe my next steps will become a next question? |
|
For all concerned - both @roosterboy's and @Greenamberred's solutions were useful. |
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
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.