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

Merge branch 'master' of gitlab.cs.umd.edu:keleher/cmsc436PublicF2021

parents 6a265cbc 711871ee
No related branches found
No related tags found
No related merge requests found
```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")
}
```
......@@ -74,9 +74,10 @@ As we did in class, you will create a new SwiftUI iOS app:
cloned from gitlab earlier).
- When creating the project:
- call it `assign1`
- **check** "unit tests", **uncheck** `git`, **uncheck** UI tests
(not fatal, but the `Test` command from the `Product` menu will
fail because it will try to test your GUI, and you don't have one.
- **check** "unit tests", **uncheck** `git`, **check** tests
(do not delete the UI tests, as some students have reported
problems with running the file when cloned to a second location
after doing so).
- Create a new file `model.swift` to implement the `Twos` class in
your app by selecting "New / File"
from the File menu, selecting the Swift file icon in the dialog, and
......@@ -177,7 +178,7 @@ the margin next to the individual tests.
# Submit, and Grading
Submit by pushing your project to your repository. **Check this**
by visiting your repository in a web browser at
`https://gitlab.cs.umd.edu/cmsc436fall2021/<dirid>`
`https://gitlab.cs.umd.edu/cmsc436fall2021/cmsc436-<dirid>`
**Alternatively:** Test that your project create and push to the
repository is correct by cloning it to a separate, throwaway
......
```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")
}
```
```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