Skip to content

Instantly share code, notes, and snippets.

View EvolvingParty's full-sized avatar

Kurt EvolvingParty

View GitHub Profile
EvolvingParty / Checkpoint9.swift
Created November 21, 2022 11:03
DAY 14. 100 Days of SwiftUI – Hacking with Swift. Checkpoint 9
import Cocoa
/// Write a function that accepts an optional array of integers, and returns one randomly. If the array is missing or empty, return a random number in the range 1 through 100.
/// Write this whole thing in one line of code
func pickANumber(from: [Int]? = nil) -> Int {
from?.randomElement() ?? Int.random(in: 1...100)
let newNuber = pickANumber()
EvolvingParty / Checkpoint8.swift
Created November 20, 2022 12:19
DAY 13. 100 Days of SwiftUI – Hacking with Swift. Checkpoint 8
import Cocoa
///Make a protocol that describes a building, adding various properties and methods, then create two structs, House and Office, that conform to it. Your protocol should require the following:
///A property storing how many rooms it has.
///A property storing the cost as an integer (e.g. 500,000 for a building costing $500,000.)
///A property storing the name of the estate agent responsible for selling the building.
///A method for printing the sales summary of the building, describing what it is along with its other properties.
protocol Building {
var numberOfRooms: Int { get }
EvolvingParty / demoApp.swift
Created November 19, 2022 23:20
How to detect if the SwiftUI application in background or foreground
import SwiftUI
struct ios14_demoApp: App {
@Environment(\.scenePhase) var scenePhase
var body: some Scene {
WindowGroup {
}.onChange(of: scenePhase) { phase in
EvolvingParty / Checkpoint7
Created November 19, 2022 13:38
DAY 12. 100 Days of SwiftUI – Hacking with Swift. Checkpoint 7
import Cocoa
///Make a class hierarchy for animals, starting with Animal at the top, then Dog and Cat as subclasses, then Corgi and Poodle as subclasses of Dog, and Persian and Lion as subclasses of Cat.
///But there’s more:
///The Animal class should have a legs integer property that tracks how many legs the animal has.
///The Dog class should have a speak() method that prints a generic dog barking string, but each of the subclasses should print something slightly different.
///The Cat class should have a matching speak() method, again with each subclass printing something different.
///The Cat class should have an isTame Boolean property, provided using an initializer.
class Animal {
EvolvingParty / Checkpoint6.swift
Created November 18, 2022 09:35
Day 11 100 Days of SwiftUI – Hacking with Swift. Checkpoint 6
import Cocoa
///Create a struct to store information about a car
///Include its model, number of seats, and current gear.
struct Car {
let model: String
let numberOfSeats: Int
private(set) var currentGear: Int {
willSet {
EvolvingParty / Checkpoint5.swift
Created November 16, 2022 05:16
DAY 9: 100 Days of SwiftUI – Hacking with Swift. Checkpoint 5
import Cocoa
///You’ve already met sorted(), filter(), map(), so I’d like you to put them together in a chain – call one, then the other, then the other back to back without using temporary variables.
///Your input is this: let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
///Your job is to:
///Filter out any numbers that are even
///Sort the array in ascending order
///Map them to strings in the format “7 is a lucky number”
///Print the resulting array, one item per line
EvolvingParty / Checkpoint4.swift
Created November 15, 2022 04:03
Day 8: 100 Days of SwiftUI – Hacking with Swift. Checkpoint 4
import Cocoa
///Write a function that accepts an integer from 1 through 10,000, and returns the integer square root of that number.
///You can’t use Swift’s built-in sqrt() function or similar – you need to find the square root yourself.
///If the number is less than 1 or greater than 10,000 you should throw an “out of bounds” error.
///You should only consider integer square roots – don’t worry about the square root of 3 being 1.732, for example.
///If you can’t find the square root, throw a “no root” error.
enum isqrError: Error {
case noRoot, outOfBounds
EvolvingParty / Checkpoint3.swift
Created November 13, 2022 23:03
100 Days of SwiftUI! DAY 6
import Cocoa
///Your goal is to loop from 1 through 100, and for each number:
///If it’s a multiple of 3, print “Fizz”
///If it’s a multiple of 5, print “Buzz”
///If it’s a multiple of 3 and 5, print “FizzBuzz”
///Otherwise, just print the number.
for i in 1...100 {
if i.isMultiple(of:3) && i.isMultiple(of:5) {