Skip to content
Snippets Groups Projects
Commit 711871ee authored by Peter J. Keleher's avatar Peter J. Keleher
Browse files

auto

parent f4bf5964
No related branches found
No related tags found
No related merge requests found
// Notes used by pete in class (not originally intended to be public,
// but I was asked.....), some of these are examples of
// correct usage, and some illustrate issues to be fixed in class. Some
// lines are comments, some are notes to myself. They are not intended
// to be correct "as is". Use them to explore on your own.
```go
func changeB(a:String, b: inout String) {
b = "\(a)\(b)"
}
var s="this"
changeB(a:"changing ", b:&s)
print(s)
//=====================================================================
func outer() -> (Int) -> Int {
var incr = 0
func inner(_ a:Int) -> Int {
incr += 1
return a+incr
}
return inner
}
let x = outer()
x(33)
//=====================================================================
Array::sorted(by: (T,T)->Bool)
let nums = [7,5,14,12,16]
// Sort with inferred types
let snums = nums.sorted(by:{a,b in a>b})
// Sort with positional parameters
let snums2 = nums.sorted(by:{$0>$1})
// Sort with function name
let snums3 = nums.sorted(by:>)
// Sort with a trailing closure
let snums4 = nums.sorted() {$0>$1}
// Sort with parentheses omitted
let snums5 = nums.sorted {$0>$1}
//=====================================================================
enum Earthsea: CaseIterable {
case AWizardOfEarthsea
case TheTombsOfAtuan
case TheFarthestShore
case Tehanu
}
for book in Earthsea.allCases {
print(book)
}
//=====================================================================
struct Point {
var x: Int = 0
var y: Int = 0
}
let p = Point(x:3, y: 4)
let p2 = Point(x:4, y: 5)
enum Shape {
case circle(center:Point, radius:Double)
case rect(lowerLeft:Point, upperRight:Point)
}
let s = Shape.circle(center:p, radius:1.5)
switch s {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
let sr = Shape.rect(lowerLeft: p, upperRight: p2)
switch sr {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
```
```swift
func changeB(a:String, b: inout String) {
b = "\(a)\(b)"
}
var s="this"
changeB(a:"changing ", b:&s)
print(s)
//=====================================================================
func outer() -> (Int) -> Int {
var incr = 0
func inner(_ a:Int) -> Int {
incr += 1
return a+incr
}
return inner
}
let x = outer()
x(33)
//=====================================================================
Array::sorted(by: (T,T)->Bool)
let nums = [7,5,14,12,16]
// Sort with inferred types
let snums = nums.sorted(by:{a,b in a>b})
// Sort with positional parameters
let snums2 = nums.sorted(by:{$0>$1})
// Sort with function name
let snums3 = nums.sorted(by:>)
// Sort with a trailing closure
let snums4 = nums.sorted() {$0>$1}
// Sort with parentheses omitted
let snums5 = nums.sorted {$0>$1}
//=====================================================================
enum Earthsea: CaseIterable {
case AWizardOfEarthsea
case TheTombsOfAtuan
case TheFarthestShore
case Tehanu
}
for book in Earthsea.allCases {
print(book)
}
//=====================================================================
struct Point {
var x: Int = 0
var y: Int = 0
}
let p = Point(x:3, y: 4)
let p2 = Point(x:4, y: 5)
enum Shape {
case circle(center:Point, radius:Double)
case rect(lowerLeft:Point, upperRight:Point)
}
let s = Shape.circle(center:p, radius:1.5)
switch s {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
let sr = Shape.rect(lowerLeft: p, upperRight: p2)
switch sr {
case .circle(let c, let r):
print("It's a circle \(c) \(r)")
case let .rect(ll, ur):
print("Just a rect \(ll) \(ur)")
default:
print("default")
}
```
// Notes used by pete in class (not originally intended to be public,
// but I was asked.....), some of these are examples of
// correct usage, and some illustrate issues to be fixed in class. Some
// lines are comments, some are notes to myself. They are not intended
// to be correct "as is". Use them to explore on your own.
```swift
struct Stack<Element> {
var items = [Element]()
mutating func push(_ item: Element) {
items.append(item)
}
mutating func pop() -> Element {
return items.removeLast()
}
}
var t = Stack<Int>()
print(t)
t.push(3)
t.push(4)
print(t.pop())
var s = Stack<String>()
s.push("nice")
print(s.pop())
//=====================================================================
extension Stack {
var top: T? {
return items.isEmpty ? nil : items[0]
}
}
if let x = t.top {
print("there is another int")
}
if let y = s.top {
print("there is another string")
}
//=====================================================================
enum MyError: Error {
case bad1
case bad2
}
func blah() throws {
defer { print("I've got a deferment") }
defer { print("Second deferment") }
throw MyError.bad1
print("regular blah")
}
do {
try blah()
} catch let e where e is MyError {
print("my bad \(e)")
}
//=====================================================================
class Person { // references: 0
let name:String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { print("\(name) is being deinitialized") }
}
class Apartment { // references: 0
let unit: String
init(unit: String) { self.unit = unit }
var tenant: Person?
deinit { print("Apartment \(unit) is being deinitialized") }
}
var john:Person? = Person(name: "John Doe")
var unit4A:Apartment? = Apartment(unit: "4A")
john!.apartment = unit4A
unit4A!.tenant = john
john = nil
unit4A = nil
// make either ref weak and they go away
// make .tenant unowned (.apartment strong)
john = nil
print(unit4A!.tenant) // crash
//=====================================================================
class Country {
let name: String
var capitalCity: City? = nil
init(nameIn: String, capitalName: String) {
name = nameIn
capitalCity = City(nameIn: capitalName, countryIn: self)
}
deinit {
print("\(name) deiniting")
}
}
class City {
let name: String
unowned let country: Country
init(nameIn: String, countryIn: Country) {
name = nameIn
country = countryIn
}
deinit {
print("\(name) deiniting")
}
}
var country :Country? = Country(nameIn: "Bangladesh", capitalName: "Dhaka")
// need to make capitalCity OPTIONAL so init can complete
// still have loop, so make country UNOWNED
//=====================================================================
class Person {
var firstName: String?
var lastName: String?
var fullName: ()->String = {
return ("\(self.firstName!) \(self.lastName!)")
}
init(f: String, l: String) {
firstName = f
lastName = l
}
deinit {
print("deiniting")
}
}
var x:Person? = Person(f: "pete", l: "keleher")
print(x!.fullName())
x = nil
print("done")
// Add 'lazy' to def of fullname, allow not violate safety check.
// add " [unowned self] in" (a "capture list") to fullName closure to avoid strong reference loop:
class Person {
var firstName: String?
var lastName: String?
lazy var fullName: ()->String = {
[unowned self]
return ("\(self.firstName!) \(self.lastName!)")
}
init(f: String, l: String) {
firstName = f
lastName = l
}
deinit {
print("deiniting")
}
}
//=====================================================================
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment