This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
@main | |
struct ios14_demoApp: App { | |
@Environment(\.scenePhase) var scenePhase | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
}.onChange(of: scenePhase) { phase in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |