Could you tell me please, how can I make something like this:
For example I have an app which contains info about your pets. But firstly, you should add a pet to a database. In a special view, you enter your pet’s name, age, breed, color and etc (keys) and then you save it. Here we are, now you have your first pet in the app. But then you wanna add new pets…
How can I save all these dictionaries with same keys ? You don’t know how may dictionaries to create because you don’t know how many pets an user wanna add in the app. What is the easiest way?
What if user wants to add like thousands of pets in the app ? I am sure, I shouldn’t create thousands of dictionaries in the code for every possible animal. But how do I do that ?
Like dictionary1 for Deisy, dictionary2 for Bruno, dictionary3 for Hamlet and etc.
Here is a rough description of how it could work:
Somewhere you need to define a collection to hold that pets.
When the user has entered the data and presses the save button, some method is called in your view controller. Within that method you collect the data, like this:
At this point the
pethas been put into themyPetsNSMutableSetand the compiler will release it, so it doesn’t exist anymore. The next time the user enters data and presses the button again, a newpetwill be created and put intomyPets. Don’t worry about millions of instances being created, the compiler will take care of it.If you want to retrieve individual pets afterwards, you can do something like this