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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Gathering inventory with collections24s
-
(Locked)
Swift arrays4m 27s
-
(Locked)
Core array methods6m 47s
-
Swift dictionaries5m 39s
-
(Locked)
Core dictionary methods7m 6s
-
(Locked)
Working with sets4m 12s
-
(Locked)
Core set methods3m 35s
-
(Locked)
Swift tuples5m 10s
-
(Locked)
Challenge: Merchant shop32s
-
(Locked)
Solution: Merchant shop5m 19s
-
(Locked)
-
-
-
-
-