UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Swift Day 2 - Question about Dictionaries

Forums > Swift

About dictionaries, I don't understand the meaning of last paragraph mentioned about: Note: When using type annotations, dictionaries are written in brackets with a colon between your identifier and value types. For example, [String: Double] and [String: String].

I need some examples to understand this paragraph, thanks.

3      

If you have var greeting = "Hello, playground" the type (of String) is inferred, however you can do var greeting: String to tell Swift that greeting is a type of String. So the note is say how to tell Swift of the type eg

var someDict: [String: String]
var otherDict: [String: Double]

4      

Type annotations <== This phrase is used a LOT in SwiftUI and Swift documentation and videos.

We mostly can guess the type of a variable when it's defined. Let's try!

let someVariable = 2   // What type are we storing here?  Have a guess!
var anotherVariable = "Please press the Like button and subscribe!"

This isn't a problem. We can see the first is an Int and the second is a String.

But in some cases, you'll not have data ready to use. But you need to reserve the variable. In this case, you'll want to LEAVE A NOTE to let Swift know what you plan to use. You will annotate your declaration with the TYPES you plan to use.

let someVariable : Int     // leaving a NOTE for Swift.
let anotherVariable : String // this is Type Annotation. 

As @nigel points out above, when you want to declare a dictionary, you can let Swift guess by providing default values. But if you want to declare a dictionary without default data, you have to use TYPE ANNOTATION to let Swift know what types you'll be using in the dictionary.

// Declare a dictionary by type inference. (Let Swift guess the types!)
var osDictionary = ["Macintosh": "MacOS", "iPhone" : "iOS", "Watch" : "WatchOS" ]  // Inferred types.
// Declare a dictionary by type annotation. (Leave a note for Swift ... )
var someDictionary: [String: String]    // This is a note for Swift
var otherDictionary: [String: Double]   // Swift doesn't have data yet, but knows what you'll be storing.

4      

Thanks @Obelix your clear and detail reply, I totally understand. Thanks @NigelGee your quick and simple reply, too. 👍

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.