diff --git a/Chapter1.txt b/Chapter1.txt deleted file mode 100644 index 714daca..0000000 --- a/Chapter1.txt +++ /dev/null @@ -1,3 +0,0 @@ -This a journey - -A digital journey that will enlighten. diff --git a/GenericsPlayground b/GenericsPlayground new file mode 100644 index 0000000..8f90dcc --- /dev/null +++ b/GenericsPlayground @@ -0,0 +1,20 @@ +//You will be given an array a and a value x. All you need to do is check whether the provided array contains the value. +// +//The type of a and x can be String or Int. +// +//Return true if the array contains the value, false if not. + +//https://www.raywenderlich.com/722-swift-generics-tutorial-getting-started + +import UIKit + +protocol Summable { static func +(lhs: Self, rhs: Self) -> Self } +extension Int: Summable {} +extension Double: Summable {} + +func add(x: T, y: T) -> T { + return x + y +} + +let addIntSum = add(x: 1, y: 2) // 3 +let addDoubleSum = add(x: 1.0, y: 2.0) // 3 diff --git a/PlaygroundProtocols b/PlaygroundProtocols new file mode 100644 index 0000000..405bff4 --- /dev/null +++ b/PlaygroundProtocols @@ -0,0 +1,191 @@ +//https://www.raywenderlich.com/814-introducing-protocol-oriented-programming-in-swift-3 + +import UIKit + +var str = "Hello, playground" + +protocol Bird : CustomStringConvertible{ + var name : String { get } + var canFly : Bool { get } +} +extension CustomStringConvertible where Self : Bird { + var description : String { + return canFly ? "I can fly" : "Guess I'll just sit here :[" + } +} + +protocol Flyable { + var airspeedVelocity : Double { get } +} + +extension Bird { + //Flyable birds can fly + var canFly : Bool { return self is Flyable } +} + + +struct FlappyBird : Bird, Flyable { + let name: String + let flappyAmplitude: Double + let flappyFrequency: Double +// let canFly = true + + var airspeedVelocity: Double { + return 3 * flappyFrequency * flappyAmplitude + } +} + +struct Penguin : Bird { + let name: String +// let canFly = false +} + +struct SwiftBird : Bird, Flyable { + var name : String { return "Swift \(version)" } + let version : Double +// let canFly = true + + //Swift is faster every version + var airspeedVelocity: Double { return version * 1000.0 } +} + +enum UnladenSwallow : Bird, Flyable { + case african + case european + case unknown + + var name: String { + switch self { + case .african: + return "African" + case .european: + return "European" + case .unknown: + return "Unknown" + } + } + + var airspeedVelocity: Double { + switch self { + case .african: + return 10.0 + case .european: + return 9.9 + case .unknown: + fatalError("You are thrown from the bridge of death") + } + } +} + +extension UnladenSwallow { + var canFly : Bool { + return self != .unknown + } +} + +UnladenSwallow.unknown.canFly +UnladenSwallow.african.canFly +Penguin(name: "King Penguin").canFly + +UnladenSwallow.african +Penguin(name: "Chin strap Penguin") +UnladenSwallow.european +UnladenSwallow.unknown + +let numbers = [10, 20, 30, 40, 50, 60] +let slice = numbers[1...3] +let reversedSlice = slice.reversed() +print (reversedSlice) + +let answer = reversedSlice.map {$0 * 10} +print (answer) + + +class Motorcycle { + init(name: String) { + self.name = name + speed = 200 + } + var name : String + var speed : Double +} + +protocol Racer { + var speed: Double { get } // speed is the only thing racers care about +} + +extension FlappyBird : Racer { + var speed : Double { + return airspeedVelocity + } +} + +extension SwiftBird : Racer { + var speed : Double { + return airspeedVelocity + } +} + +extension Penguin : Racer { + var speed : Double { + return 42 // full waddle speed + } +} + +extension UnladenSwallow : Racer { + var speed : Double { + return canFly ? airspeedVelocity : 0 + } +} + +extension Motorcycle : Racer {} + +let racers : [Racer] = +[UnladenSwallow.african, +UnladenSwallow.european, +UnladenSwallow.unknown, +Penguin(name: "King Penguin"), +SwiftBird(version: 4.2), +FlappyBird(name: "Filipe", flappyAmplitude: 3.0, flappyFrequency: 20.0), +Motorcycle(name: "Giacomo") +] + +func topSpeed(of racers: RacerType) -> Double + where RacerType.Iterator.Element == Racer { + return racers.max(by: { $0.speed < $1.speed })?.speed ?? 0 +} + + + +func topSpeed ( of racers: [Racer]) -> Double { + return racers.max(by: { $0.speed < $1.speed })?.speed ?? 0 +} + +topSpeed(of: racers[1...3]) + +extension Sequence where Iterator.Element == Racer { + func topSpeed() -> Double{ + return self.max(by: { $0.speed < $1.speed })?.speed ?? 0 + } +} + +racers.topSpeed() +racers[1...3].topSpeed() + +protocol Score : Equatable , Comparable { + var value: Int {get} +} + +struct RacingScore: Score { + let value: Int + + static func == (lhs: RacingScore, rhs: RacingScore) -> Bool { + return lhs.value == rhs.value + } + + static func < (lhs: RacingScore, rhs: RacingScore) -> Bool { + return lhs.value < rhs.value + } +} + +RacingScore(value: 150) >= RacingScore(value: 130) diff --git a/README.md b/README.md index 6a3081b..247158c 100644 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -# Coding-Coding-Coding \ No newline at end of file +# Coding-Coding-Coding - Swift +A collection of programming elements that can enlighten + +Throughout my Swift journey, I have learned a lot and it is just the beginning. More than a few spellbinding head scratching moments (HSMs) have eventually bore fruit, and I thougt it might be useful to share those HSMs in the hope that it may speed you to finding solutions and answers to the questions you seek. diff --git a/Swift.md b/Swift.md new file mode 100644 index 0000000..e0f8e81 --- /dev/null +++ b/Swift.md @@ -0,0 +1,38 @@ +# Swift + +# Calculate the value of a number raised to a power. +https://iswift.org/cookbook/calculate-power-of-a-number + +# Code block highlighting +Double-clicking on a curly bracket in Xcode 10 highlights the affected block of code. Cool! + +# Rounding numbers and manipulating data types +Discovered the rounded() instance method today (https://developer.apple.com/documentation/swift/floatingpoint/2295900-rounded#discussion) and how creating a constant can allow the casting of data types more easily. + +var numberOfBottles = totalVolume / volumeEachBottles +numberOfBottles.rounded(.up) +or +numberOfBottles.rounded(.down) + +# Why "override func"? Overriding declaration requires an 'override' keyword. - +Answer: https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html#//apple_ref/doc/uid/TP40014097-CH17-ID196 + +# Xcode Intellisence - explained +Ever wondered what those funny symbols mean? +https://stackoverflow.com/questions/6662395/xcode-intellisense-meaning-of-letters-in-colored-boxes-like-f-t-c-m-p-c-k-etc + +# JSON Editor +Courtesy of #LondonAppBrewer this little gem of a link will bring clarity to the JSON objects you work with. +http://jsoneditoronline.org/ + +# Swift development tutorial +https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ + +# Apple developer archive +https://developer.apple.com/library/archive/navigation/ + +# Side-load with WiFi +How cool is that?! You can now side-load apps with WiFi! No more cables. Wa Hoo! @LondonAppBrewer #100DaysOfCode First connect the iPhone & computer with the lightning cable. Then in Simulator menu go to Hardware > Device > Manage Devices... then Tick Connect via Network. Do disconnect the cable from the iPhone and commence side-loading. + +# SQL-lite +Just installed Datum Free (https://itunes.apple.com/gb/app/datum-free/id901631046?mt=12 …) to get a view into SQL lite data in the ToDoey App exercise (& all that CRUD 😉) with @LondonAppBrewer #100DaysOfCode diff --git a/Switch Case Playground files b/Switch Case Playground files new file mode 100644 index 0000000..a97eda3 --- /dev/null +++ b/Switch Case Playground files @@ -0,0 +1,97 @@ +import UIKit + +// Personalized greeting +// +// Create a function that gives a personalized greeting. This function takes two parameters: name and owner. +// +// Use conditionals to return the proper message: +// +// case return +// name equals owner 'Hello boss' +// otherwise 'Hello guest' +//https://www.hackingwithswift.com/files/pro-swift-sample.pdf +// + + +var str = "Hello, playground" +let name = "fourCandles" + +switch name { +case "ronnie": + print("Hello, Ronnie Barker!") + +case "fourCandles": + print("Hello, Ronnie Corbett!") +default: + print( "Autentication failed") +} + +//Matching two values + +let password = "C0rb3tt" +let ipAddress = "192.168.0.1" + +let authentication = (name, password, ipAddress) + +switch authentication { +//case (_, _, _): //using "_" for all values will not allow matching of any of the other cases +// print("You could be anybody!") +case ("ronnie", "b@rk3r", _): //"_" here means that the third value is not required for a match + print("Hello, Ronnie Barker!") +case ("fourCandles", let password, _): //the let keyword allows a value to be recovered + print("Hello, Ronnie Corbett your password was \(password)") +default: + print("Who are you?") +} + +//Using claculated values + +func foxTrot(number: Int) -> String{ + switch (number % 7 == 0, number % 11 == 0) { + case (true, true): + return "You win FoxTrot" + case (true, false): + return "Fox" + case (false, true): + return "Trot" + case (false, false): + let badTry = String(number) + return badTry + " is not a winning number. Try again." + } +} +print (foxTrot(number: 2)) + + +// Creating and looping through an array + +let fourCandles = (name: "fourCandles", password: "C0rb3tt") +let ronnie = (name: "ronnie", password: "b@rk3r") +let fred = (name: "wilma", password: "b@m8@m") + +let users = [fourCandles, ronnie, fred] + +for user in users { + print(user.name) +} +//a case can also be applied to a for loop to match specific criteria +for case ("fourCandles", "C0rb3tt") in users { + print("fourCandles has the password \(password).") +} +//assigning the values to their individual constants +for case (let name, let password) in users { + print("User \(name) has the password \(password)..") +} + +//and more sucsinctly + +for case let (name, password) in users { + print("User \(name) has the password \(password)...") +} + +//to filter on password and have a constant to manipulate +for case let (name, "b@m8@m") in users { + print ("User \(name) has the password \"b@m8@m\"...." ) +} + + +