100 Days of SwiftUI Learning — Day 4

Krishna
1 min readMay 4, 2022

“For changes to be of any true value, they’ve got to be lasting and consistent.” - Tony Robbins.

Agenda

  1. Type Annotations
  2. Checkpoint 2

Type Annotations

Type annotations let us be explicit about what data type we want when declaring a variable or constant.

let variable_name: data_type = value

Here are the reasons why you use type annotations

  1. Swift can’t figure out what type should be used.
  2. You want swift to use a different type from its default type.
  3. You don’t want to assign a value just yet.

Declaring just the data type of a variable or constant

Integer: let constant_name : Int

Double: let constant_name:Double

String: let constant_name: String

Bool: let constant_name: Bool

Array: let constant_name:[data_type]

Dictionary: let constant_name: [key_data_type : value_data_type]

Set: let constant_name: Set<data_type>

When you declare a constant using type annotation, swift won’t allow you to accidentally use that constant before assigning it a value. Once you assign the value, you cannot change it.

Checkpoint 2

Challenge

  1. Create an array of strings with duplicates.
  2. Print number of items in the array.
  3. Get number of unique items in the array.

Solution

//Creating an array
let letters = ["a","b","c","a","d","b","e"]
//Number of items in the array
print(letters.count)
//Getting unique items of the array using set
let
uniqueLetters = Set(letters)
//Number of unique items in the array
print(uniqueLetters.count)

There wasn’t a lot of content to study today. Take a break, reflect on what you have learned and come tomorrow to continue learning.

That is everything for today. See you tomorrow.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Krishna
Krishna

No responses yet

Write a response