Home

lovelejess

21 Aug 2018

swift4: deeper dive- types

Currently enrolled in Udemy’s- The Complete iOS 11 & Swift Developer Course Notes from Lesson 4

What is Swift?

Notes from Apple’s Swift 4 Developer page

  • used to develop iOS, macOS, watchOS, and tvOS
  • released in 2014 and is open source
  • built with the LLVM compiler
  • easy to learn:
    • interactive and fast feedback with Swift Playgrounds & REPL (Read, Eval, Print, Loop) in Xcode
  • designed for safety
    • automatic memory management
    • type safety
    • null safety

Using var vs let

  • var is to indicate that the variable’s value may change over it lifetime
  • let is to indicate that the variable’s value may NOT change over its lifetime
  • best practice is to use let whenever possible, because it prevents unintended value changes

Type Conversions

  • can convert types into other types by Type(yourVariable)
  • e.g.
    let myFloat1 = 9.02 
    print(String(myFloat1))
    
  • can explicity declare the types of variables
    • helpful when you want to declare an array of doubles, but not give them decimal values
      • e.g.
        var myDouble: [Double] = [1,2,3]
        //these are actually doubles [1.0, 2.0, 3.0]
        

Arrays

  • cool things to note:
    • although not recommended, can have mix typed arrays, where its type is Any
      • example: let mixArray: Any = ["hey", 0, false]
    • initializing an empty array of a type: let stringArray = [String]()

Dictionaries

  • syntax of a dictionary;
    • let dictionary = ["key": "value"]
  • optionals are returned when retrieving a value from a dictionary as a default :
    • print(dictionary["key"])
  • or can force unwrap it and return the raw value, if you know for certain that they key exists:
    • print(dictionary["key"]!)

Til next time,
lovelejess at 10:36

scribble