![]() Notice that if we are declaring a dictionary by assigning an empty dictionary, the declaration part of the code must specify the type! Mutability ![]() Var myDictionary: = // assigning an empty dictionary Var myDictionary: = // assigning value and speficying the typeĭeclaring and initializing empty dictionaries by assigning an empty dictionary: var myDictionary = () // assigning a default value of type initialization Var myDictionary: Dictionary // declaration by specifying type - full versionĭeclaring initializing by assigning a value, with or without specifying a type: var myDictionary = // assigning value and inferring the type We've already experimented with a couple of variants, so now let's sum them up.ĭeclaring dictionaries by specifying type: var myDictionary: // declaration by specifying type - short option Var truth = // Working with Dictionaries Declaration and initialization Var confusingInfo = // Errorīut the the type of keys may be different from the type of elements or may be the same: var professions = // Like for simple variable types and arrays, the same rules apply to dictionaries: it always has a type and it cannot change its type.Īll elements in a dictionary must be of the same type. And all keys must be of the same type: var ages = // Ok Like for arrays, you can also use a full declaration: Dictionary , however it's unnecessary and the short version is recommended instead. This helps me identify the purpose of my declaration - k stands for key. This is a personal preference you may come up with your own if you believe it will improve readability and will likely be understood by other developers. Swift will not allow for the mistyping of a constant's name (or a variable), but the contents of strings is solely the developer's responsibility.ĭevelop your style! As you have noticed, I have named my constants using prefix k in front of each name. One of the tricks to protect against such situations is using constants to specify the keys once and then reusing them throughout the code: // define keys as constants For example, "Jenny" and "jenny" are two different keys that will associate with two different elements in a dictionary. ![]() While using strings as keys, we must remember that such keys are case sensitive. Var ages = All keys in a dictionary must be unique. That's done by adding the key type in front of the element type followed by a colon: : // my friends' ages However, in addition to the element type we need to specify the key type. Similarly to arrays, to declare a dictionary we also use square brackets. Here's an example of a list of elements organized by key: Example of a dictionary containing people and their ages Dictionary syntax The most commonly used type is String. Which is handy in our case, because we can replace index 0 with “coffee”, 1 with “dessert”, and, 2 with “food”. This is called a KEY VALUE association: Ī dictionary may contain any number of elements, including ZERO: an empty dictionary. Luckily, Dictionaries come to the rescue! Dictionary data typeĪ dictionary is a list of elements ordered by a key.Įach element is associated with a given key, instead of a number that it happens to follow in the general order of a list. ![]() Which, in turn, would increase the results for food, producing false information for Jenny :'(. It's easy to forget or to simply mistype, using 2 instead. To increase the profit number for dessert we’ll have to remember to use index 1. The order in which we originally arranged them It works just fine, except that we always have to remember 2 things: We’ve got 3 elements that we can access using indices: 0 for coffee, 1 for dessert and 2 for food. ![]() So, let’s refer back to our solution for Jenny using arrays. Next in line are dictionaries, which are, as you may guess, the subject of this chapter! Remember, we listed 3 types of collections: Arrays, Dictionaries and Sets. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |