Completed 100 Days of Swift - Day #2

This commit is contained in:
youngchief btw ツ 2020-04-10 02:51:43 +00:00
parent 411ed5fad7
commit 57eaa6b53b
19 changed files with 143 additions and 0 deletions

View File

@ -1,3 +1,4 @@
// Constants
// https://www.hackingwithswift.com/sixty/1/6/constants
let taylor = "swift"

View File

@ -1,4 +1,5 @@
// Doubles & Booleans
// https://www.hackingwithswift.com/sixty/1/4/doubles-and-booleans
var pi = 3.141 // This is a double
var awesome = true // This is a boolean

View File

@ -1,4 +1,5 @@
// Multi-line strings
// https://www.hackingwithswift.com/sixty/1/3/multi-line-strings
var str1 = """
This goes

View File

@ -1,4 +1,5 @@
// String Interpolation
// https://www.hackingwithswift.com/sixty/1/5/string-interpolation
var score = 85
var str = "Your score was \(score)"

View File

@ -1,4 +1,5 @@
// Strings & Integers
// https://www.hackingwithswift.com/sixty/1/2/strings-and-integers
var str = "Hello, World!"
var age = 38

View File

@ -1,4 +1,5 @@
// Type Annotations
// https://www.hackingwithswift.com/sixty/1/7/type-annotations
let str = "Hello, World!"
let album: String = "Reputation"

View File

@ -1,4 +1,5 @@
// Variables
// https://www.hackingwithswift.com/sixty/1/1/variables
var str = "Hello, World!"
str = "Goodbye"

View File

@ -0,0 +1,11 @@
// Arrays vs sets vs tuples
// https://www.hackingwithswift.com/sixty/2/4/arrays-vs-sets-vs-tuples
// If you need a specific, fixed collection of related values where each item has a precise position or name, you should use a tuple:
let address = (house: 555, street: "Taylor Swift Avenue", city: "Nashville")
// If you need a collection of values that must be unique or you need to be able to check whether a specific item is in there extremely quickly, you should use a set:
let set = Set(["aardvark", "astronaut", "azalea"])
// If you need a collection of values that can contain duplicates, or the order of your items matters, you should use an array:
let pythons = ["Eric", "Graham", "John", "Michael", "Terry", "Terry"]

View File

@ -0,0 +1,10 @@
// Arrays
// https://www.hackingwithswift.com/sixty/2/1/arrays
let john = "John Lennon"
let paul = "Paul McCartney"
let george = "George Harrison"
let ringo = "Ringo Starr"
let beatles = [john, paul, george, ringo]
beatles[1]

View File

@ -0,0 +1,21 @@
// Creating empty collections
// https://www.hackingwithswift.com/sixty/2/7/creating-empty-collections
// If you want to create an empty collection just write its type followed by opening and closing parentheses. For example, we can create an empty dictionary with strings for keys and values like this:
var teams = [String: String]()
// We can then add entries later on, like this:
teams["Paul"] = "Red"
// Similarly, you can create an empty array to store integers like this:
var results = [Int]()
// The exception is creating an empty set, which is done differently:
var words = Set<String>()
var numbers = Set<Int>()
// This is because Swift has special syntax only for dictionaries and arrays; other types must use angle bracket syntax like sets.
// If you wanted, you could create arrays and dictionaries with similar syntax:
var scores = Dictionary<String, Int>()
var results = Array<Int>()

View File

@ -0,0 +1,11 @@
// Dictionaries
// https://www.hackingwithswift.com/sixty/2/5/dictionaries
// Dictionaries are collections of values just like arrays, but rather than storing things with an integer position you can access them using anything you want.
let heights = [
"Taylor Swift": 1.78,
"Ed Sheeran": 1.73
]
heights["Taylor Swift"] // Access value of "Taylor Swift" which is 1.78

View File

@ -0,0 +1,10 @@
// Dictionary default values
// https://www.hackingwithswift.com/sixty/2/6/dictionary-default-values
let favoriteIceCream = [
"Paul": "Chocolate",
"Sophie": "Vanilla"
]
favoriteIceCream["Paul"]
favoriteIceCream["Charlotte", default: "Unknown"]

View File

@ -0,0 +1,11 @@
// Enum associated values
// https://www.hackingwithswift.com/sixty/2/9/enum-associated-values
enum Activity {
case bored
case running(destination: String)
case talking(topic: String)
case singing(volume: Int)
}
let talking = Activity.talking(topic: "football")

View File

@ -0,0 +1,18 @@
// Enum raw values
// https://www.hackingwithswift.com/sixty/2/10/enum-raw-values
enum Planet: Int {
case mercury
case venus
case earth
case mars
}
let earth = Planet(rawValue: 2)
// enum Planet: Int {
// case mercury = 1
// case venus
// case earth
// case mars
// }

View File

@ -0,0 +1,14 @@
// Enumerations aka enums
// https://www.hackingwithswift.com/sixty/2/8/enumerations
// A way of defining groups of related values in a way that makes them easier to use.
let result = "failure"
let result2 = "failed"
let result3 = "fail"
enum Result {
case success
case failure
}
let result4 = Result.failure

View File

@ -0,0 +1,14 @@
# Day #2
---
**What I learned**
- **[Arrays](/100DaysOfSwift/Day2/Arrays.swift)**
- **[Sets](/100DaysOfSwift/Day2/Sets.swift)**
- **[Tuples](/100DaysOfSwift/Day2/Tuples.swift)**
- **[Arrays vs sets vs tuples](/100DaysOfSwift/Day2/Arrays%20vs%20sets%20vs%20tuples)**
- **[Dictionaries](/100DaysOfSwift/Day2/Dictionaries.swift)**
- **[Dictionary default values](/100DaysOfSwift/Day2/Dictionary%20default%20values)**
- **[Creating empty collections](/100DaysOfSwift/Day2/Creating%20empty%20collections.swift)**
- **[Enumerations](/100DaysOfSwift/Day2/Enumerations.swift)**

View File

@ -0,0 +1,5 @@
// Sets
// https://www.hackingwithswift.com/sixty/2/2/sets
let colors = Set(["red", "green", "blue"]) // Sets can be created from arrays.
let colors2 = Set(["red", "green", "blue", "red", "blue"] // Sets can't have duplicate items. They would just get ignored.

View File

@ -0,0 +1,6 @@
// Tuples
// https://www.hackingwithswift.com/sixty/2/3/tuples
var name = (first: "Taylor", last: "Swift")
name.0
name.first

View File

@ -5,3 +5,8 @@
**This folder is for the [100 Days of Swift](https://www.hackingwithswift.com/100) challenge.**
**NOTE:** This is **[100 Days of Swift](https://www.hackingwithswift.com/100)**, not **[100 Days of SwiftUI](https://www.hackingwithswift.com/100/swiftui)** which is located [here](/100DaysOfSwiftUI/)
---
- **[Day #1](/100DaysOfSwift/Day1/)**
- **[Day #2](/100DaysOfSwift/Day2/)**