2 mistakes?! Swift Playground Lesson 11 Making Decisions

I'm a beginner, started a couple of days ago, just doing it for fun, so maybe I'm wrong but seems to me there are at least 2 mistakes in Apple's Intro to App Development !


1 - in the Swift playground file 11_MakingDecisions

2 - in the iBook Intro to App Development with Swift Chapter 11



1 - Swift playground file > 11_MakingDecisions > Functions and Decisions


This is the code


func bandCanCarryGear(bandMemberCount: Int, gearWeight: Int) -> Bool {
    let maximumTripCount = 2
    let weightPerPerson = 50
    let carryingCapacity = bandMemberCount * weightPerPerson * maximumTripCount
    return gearWeight < carryingCapacity
}

if bandCanCarryGear(bandMemberCount: 5, gearWeight: 600) {
  "Rock on."
} else {
    "Everyone quits! Looks like you've got a solo show"
}


which, when bandMemberCount is set to 5, resolves to "Everyone quits! Looks like you've got a solo show".

So far so good.

But then the lesson finishes with


Now anyone reading the code should be able to understand what it’s doing. (It looks like you need to hire another drummer, or leave some speakers behind.)


But if you increase bandMemberCount to 6, you get the same message because gearWeight (i.e. 6 * 2 * 50) = carryingCapacity (not <), therefore the top function still returns false.


QED Adding ONE more drummer makes no difference. Either add TWO drummers or change this line


return gearWeight < carryingCapacity

to

return gearWeight <= carryingCapacity



2 - iBooks - Intro to App Development with Swift > Chapter 11, page 76 - Check your understanding Question 3 of 3


This is the code in a multiple choice question


let numbers = 100
if numbers > 99 {
    print("What a smallish number!")
} else if numbers < 150 {
    print("That's not so big.")
}
print("Goodbye.")


What will the code snippet print out? (4 choices)


A - "What a smallish number!"

B - "What a smallish number!" "Goodbye."

C - "What a smallish number!" "That's not so big." "Goodbye."

D - "That's not so big." "Goodbye."


They give the correct answer as D

I think the correct answer is B, and I've run the code online to check and it gives B.


The reason is that the if statement stops on the first true statement (100 > 99) and never gets to the second statement (100 < 150).


If I am correct on these two points, I'd appreciate someone more knowledgeable confirming. If I'm right, the multiple choice is a particularly bad mistake in a reference book as it seems to teach the opposite of what is true.


Also if Apple is reading this and are unaware of these errata and wish to reward me with an Apple Pencil bounty I will not insult their magnanimity by refusing 🙂Else, knowledge is its own reward 🙂

Replies

More bugs in the Swift playground

Intro to App Development Curriculum > 12_InstancesMethodsProperties > Exercise-Freight Elevator


This is the Exercise:


Use the TreehousePulley type's addLoadToBasket method to add the items you defined above. Add three of the lightest item, two of the middle-weight item, and one of the heaviest item. Add the items to the lightweight pulley first, using the canHandleAdditionalLoad method to check whether the item would overload the pulley, then move on to your stronger pulley when the first is fully loaded.

If your pulleys together aren't enough to hold all the items you need, create a third super heavy-duty pulley to finish the job.


Bugs

1. there is no .addLoadToBasket method that comes up with autocomplete

2. .canHandleAdditionalLoad seems inconsistent, returns false when it shouldn't (XCode looks like it's not responding)

I see these 2 error messages

1. Swift Compiler Error

/Desktop/Intro to App Development Curriculum/12_InstancesMethodsProperties.playground/Pages/Exercise-Freight Elevator.xcplaygroundpage:24:13: 'addLoadToBasket' is inaccessible due to 'internal' protection level

2. Uncategorized

/Desktop/Intro to App Development Curriculum/12_InstancesMethodsProperties.playground/Pages/Exercise-The Return of BoogieBot.xcplaygroundpage/Sources/BoogieBotLayer.swift:36:87: 'M_PI_4' is deprecated: Please use 'Double.pi / 4' or '.pi / 4' to get the value of correct type and avoid casting.


this is the code I tried

let lightWeight = 70
let mediumWeight = 450
let heavyWeight = 2500

let ricketyRope = TreehousePulley(weightCapacity: 200)
let strongRope= TreehousePulley(weightCapacity: 2000)

ricketyRope.addLoadToBasket(lightWeight)
strongRope.addLoadToBasket(lightWeight)

ricketyRope.canHandleAdditionalLoad(lightWeight)
strongRope.canHandleAdditionalLoad(heavyWeight)


this is the code I found in Sources (lots of unfamiliar terms for me)


import Foundation
public struct TreehousePulley: CustomStringConvertible {
    public let weightCapacity: Int
    private var currentWeight: Int
    public init(weightCapacity: Int) {
        self.weightCapacity = weightCapacity
        self.currentWeight = 0
    }
    public func canHandleAdditionalLoad(_ load: Int) -> Bool {
        if currentWeight + load > weightCapacity {
            return true
        } else {
            return false
        }
    }
    mutating func addLoadToBasket(loadWeight: Int) {
        currentWeight += loadWeight
    }
    mutating func removeLoad(loadWeight: Int) {
        currentWeight -= loadWeight
    }
    public var description: String {
        return "capacity: \(weightCapacity); current load: \(currentWeight)"
    }
}

There appear to be two problems here.

1.

public func canHandleAdditionalLoad(_ load: Int) -> Bool {
      // if currentWeight + load > weightCapacity {
          if currentWeight + load < weightCapacity {
        return true
        } else {
            return false
        }
    }


> (greater than) operator looks wrong here - surely it has to be < (less than). This guide seems to have real difficulty with greater than and less than! What with the bizarre mnemonic about eating a hamburger with a hungry mouth, I think the author has become confused.


2.


mutating func addLoadToBasket(loadWeight: Int) {
        currentWeight += loadWeight
    }
mutating func removeLoad(loadWeight: Int) {
        currentWeight -= loadWeight
    }


there is some problem with mutating / public / private in these functions with self earlier. This is beyond my understanding but the two protection levels (?) seem incompatible. I tried playing around with them but couldn't find a combo that worked.

Same problem. I thought that I dont understand something, but turns out I'm not the only with this mistake.

Did you report this problens to Apple?

Glad to see that someone else has questioned the answer to problem three in "Making Decisions", Chapter 11, page 76.

A confirmation from Apple would be much appreciated...

It looks like the update on Sept 21 to add Swift 4 also fixed this error. I came looking for this because my iBooks were apparently not updating in High Sierra so I had the old version until I deleted and downloaded again. (now that i did that, the updates button has reappeared and all my Apple Education books updated) Hoping the other bugs i've been reading about in future chapters are fixed too.


Cheers!

Yes, I just noticed the same error in the marking of chapter 11, question 3.


There was another error in chapter 9, when marking the question that reads: 'When you're reading code and aren't sure of the type of a variable or constant, what's the quickest way to find out?' The correct answer is 'B. Option-click the constant's or variable's name in Xcode', but it got marked wrong.


Looks like someone was a bit sloppy when they put these quizes together.

1. Agree with the first one; comparison operator should have been 'less than'


2. I played around with this also. Answer seems to be to just add the keyword "public" to the start of the function definition in the source code behind the playground. Then it stops being an "internal" function and you have external access. Code seems to work for me now.