From the course: Swift 6 Essential Training

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Core dictionary methods

Core dictionary methods - Swift Tutorial

From the course: Swift 6 Essential Training

Core dictionary methods

- [Instructor] The next logical step with dictionaries is learning how to update, remove, and otherwise modify their values. Let's create a new dictionary to work with. I'm going to call this player stats, and this is going to be string keys and int values just like last time. I'm also going to give it some initial values, so I'm going to say HP is that 100, attack is at 35, and gold is at 29. I'm also going to add a print statement down in our caching and removing items comment, say print playerStats. Perfect. In some cases you may want to store an overwritten value when updating a key value pair, which isn't possible with subscripts. For that we need to use the update value method. So we're going to capture the overwritten value in a variable called old value. We're going to say player stats, update value. So let's declare a new variable called old value, and this is going to store our overwritten value from player…

Contents