DEV Community

BC
BC

Posted on

Learn SwiftUI (Day 4/100)

Swift

  • Type annotations
import Cocoa

// ## Type annotations

let surname: String = "Lasso"
var score: Int = 0
var isAuthenticated: Bool = true
var albums: [String] = ["Red", "Fearless"]
var user: [String: String] = ["id": "@twostraws"]
var books: Set<String> = Set(["Dune 1", "Dune 2"])
// empty string array
var cities: [String] = []
// or
var clues = [String]()

// it is legit that declare the type to a constant and assign its value later
let username: String
// lots of complex logic
username = "@twostraws"
// lots more complex logic
print(username)

// ## Checkpoint 2
/*
 Create an array of strings,
 then write some code that prints the number of items
 in the array and also the number of unique items in the array.
 */

var names = ["John", "Doe", "John", "Mike"]
print(names.count)
print(Set(names).count)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)