From 40dead42989bce55f588ac1ee975dd4e50ac2c56 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Fri, 19 Oct 2018 13:50:03 +0300 Subject: [PATCH 01/17] Create chapter 2 --- chapter2.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 chapter2.txt diff --git a/chapter2.txt b/chapter2.txt new file mode 100644 index 0000000..e68ff00 --- /dev/null +++ b/chapter2.txt @@ -0,0 +1 @@ +The adventure. From 1d3b0c3a28b7b779492e1b7b67964284ad1fb604 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Thu, 25 Oct 2018 16:05:00 +0300 Subject: [PATCH 02/17] New Swift Trials & Tribulations --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a3081b..44b013f 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -# Coding-Coding-Coding \ No newline at end of file +# Swift + +# 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 From 0fdf9eb54c93c2bba40a73b2709a3f478b725c51 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Thu, 25 Oct 2018 16:12:17 +0300 Subject: [PATCH 03/17] Rename README.md to Swift.md --- README.md => Swift.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.md => Swift.md (100%) diff --git a/README.md b/Swift.md similarity index 100% rename from README.md rename to Swift.md From 9d3689032196a42e214b2d0d1ad707a722d42624 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Thu, 25 Oct 2018 16:12:47 +0300 Subject: [PATCH 04/17] Delete Chapter1.txt --- Chapter1.txt | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Chapter1.txt 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. From f607bbc294471be9dd579d51aa74edc294f08582 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Thu, 25 Oct 2018 16:13:00 +0300 Subject: [PATCH 05/17] Delete chapter2.txt --- chapter2.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 chapter2.txt diff --git a/chapter2.txt b/chapter2.txt deleted file mode 100644 index e68ff00..0000000 --- a/chapter2.txt +++ /dev/null @@ -1 +0,0 @@ -The adventure. From c0e3326f9adf703cb1a2eee73163d62072a16f08 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Thu, 25 Oct 2018 16:19:14 +0300 Subject: [PATCH 06/17] Create README.md Describe the purpose of this repo --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..db953cf --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Coding-Coding-Coding - Swift +A collection of programming elements that can enlighten + +Throughout my Swift journey, I have learned a lot. I have also had a few head scratching moments (HSMs) and I thougt it might be useful to share those HSMs in the hope that it may mitigate similar suffering elsewhere. From 244a94eb704e945bb44df259f440793578764250 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Fri, 26 Oct 2018 09:28:18 +0300 Subject: [PATCH 07/17] Update README.md Improve readability and add spice. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index db953cf..247158c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ # Coding-Coding-Coding - Swift A collection of programming elements that can enlighten -Throughout my Swift journey, I have learned a lot. I have also had a few head scratching moments (HSMs) and I thougt it might be useful to share those HSMs in the hope that it may mitigate similar suffering elsewhere. +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. From c3e73781ca3db1aac3922dbb32d37050ff659016 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Fri, 26 Oct 2018 09:31:36 +0300 Subject: [PATCH 08/17] Update Swift.md Add piece about JSON --- Swift.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Swift.md b/Swift.md index 44b013f..78b0a32 100644 --- a/Swift.md +++ b/Swift.md @@ -6,3 +6,7 @@ Answer: https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html#//apple # 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/ From 8b3ab47f61a7d01a5e89b641fe216f9277958979 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Mon, 29 Oct 2018 22:14:58 +0200 Subject: [PATCH 09/17] Update Swift.md Add Dev tutorial & Apple Archive links --- Swift.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Swift.md b/Swift.md index 78b0a32..5eb3bfe 100644 --- a/Swift.md +++ b/Swift.md @@ -10,3 +10,9 @@ https://stackoverflow.com/questions/6662395/xcode-intellisense-meaning-of-letter # 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/ From dddd511c722e0e96af795b694e6ab7006622dbb3 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Tue, 30 Oct 2018 18:35:32 +0200 Subject: [PATCH 10/17] Update Swift.md Add WiFi side-load and SQL-lite --- Swift.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Swift.md b/Swift.md index 5eb3bfe..2c59f0c 100644 --- a/Swift.md +++ b/Swift.md @@ -16,3 +16,9 @@ https://developer.apple.com/library/archive/referencelibrary/GettingStarted/Deve # 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 From f269db4007f76b570e8feb973b1e5c9348f078a4 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Mon, 5 Nov 2018 11:38:06 +0200 Subject: [PATCH 11/17] Update Swift.md Add rounding and data type manipulation --- Swift.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Swift.md b/Swift.md index 2c59f0c..d235626 100644 --- a/Swift.md +++ b/Swift.md @@ -1,5 +1,8 @@ # Swift +# 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. + # 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 From 2e670a33e27db6a6d5631707da05a1753b66d05b Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Mon, 5 Nov 2018 11:42:57 +0200 Subject: [PATCH 12/17] Update Swift.md Add rounded example --- Swift.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Swift.md b/Swift.md index d235626..4d17d2d 100644 --- a/Swift.md +++ b/Swift.md @@ -3,6 +3,11 @@ # 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 From f1d594a81013cc94c6f3fc247a73ecf842a49ede Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Fri, 9 Nov 2018 10:26:04 +0200 Subject: [PATCH 13/17] Update Swift.md add code block highlighting --- Swift.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Swift.md b/Swift.md index 4d17d2d..ff61d6e 100644 --- a/Swift.md +++ b/Swift.md @@ -1,5 +1,8 @@ # Swift +# 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. From e154513d0e059b895d5e0554b5820dfaefaef159 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Mon, 12 Nov 2018 13:50:30 +0200 Subject: [PATCH 14/17] Switch Case Playground files Playground files exploring capabilities of switch case --- Switch Case Playground files | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Switch Case Playground files 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\"...." ) +} + + + From a9b8d0e2d05f7d157de4afa7cfcee26a350102f7 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Mon, 12 Nov 2018 18:56:04 +0200 Subject: [PATCH 15/17] Create GenericsPlayground Link to Gemma Barlow's generics tutorial at RayWenderlichs.com --- GenericsPlayground | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 GenericsPlayground 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 From 765f0a3a3f7882b46db657b2be440f7d6690ac86 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Tue, 13 Nov 2018 11:28:11 +0200 Subject: [PATCH 16/17] Create PlaygroundProtocols Protocols tutorial from Niv Yahel at //https://www.raywenderlich.com/814-introducing-protocol-oriented-programming-in-swift-3 --- PlaygroundProtocols | 191 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 PlaygroundProtocols 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) From f272a3f6bb3cd44e9bdec49a467b905358939a85 Mon Sep 17 00:00:00 2001 From: William Spanfelner <42092799+Will-1-Am@users.noreply.github.com> Date: Sat, 17 Nov 2018 15:47:20 +0200 Subject: [PATCH 17/17] Update Swift.md Add power! --- Swift.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Swift.md b/Swift.md index ff61d6e..e0f8e81 100644 --- a/Swift.md +++ b/Swift.md @@ -1,5 +1,8 @@ # 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!