BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

Checkpoint 8 works but still has some weird things when printing. What am I doing wrong?

Forums > 100 Days of SwiftUI

Hey fellas,

I have finished checkpoint 8 and everything seems to work fine but I see some weirdness when printing the result of my code. Does anyone know what might be wrong?

Here is the code with comments for reference :

protocol Building {
    // Building's bastic properties ↓
    var rooms: Int { get set }
    var price: Int { get set }
    var realEstateAgent: String{ get set }

    // Sales summary method ↓
    func salesSummary()
}

struct OfficeBuilding: Building {
    var rooms: Int
    var price: Int
    var realEstateAgent: String
    func salesSummary () {
        print("An office building of \(rooms) rooms has been sold by \(realEstateAgent) for $\(price)")
    }
}

struct ResidentialBuilding: Building {
    var rooms: Int
    var price: Int
    var realEstateAgent: String
    func salesSummary () {
        print("A residential building of \(rooms) rooms has been sold by \(realEstateAgent) for $\(price)")
    }
}

let wework = OfficeBuilding(rooms: 12, price: 120000000, realEstateAgent: "Adam Neuman")

print(wework.salesSummary())// can't remove the 2 parenthesis there so it prints "An office building of 12 rooms has been sold by Adam Neuman for $120000000()". If I remove the 2 parenthesis it only prints "(function)". What am I doing wrong?

   

I haven't looked at checkpoint 8 but you are using two 'print' One in the 'salesSummary' method and then again in your final line.

Change 'print(wework.salesSummary())' to 'wework.salesSummary()'

   

To make things clear you have a struct struct OfficeBuilding. In that struct you have a method func salesSummary that prints some message. How do you call a function/method in Swift? You call it adding parentheses, right? i.e. salesSummary(). When you place wework.salesSummary in print without parentheses you are not calling a method. You just print what wework.salesSummary is and salesSummary is a function, so it prints that literally 'function'. But when you add parentheses your function is called and what is inside it, is executed.

   

Yes that's it. I had forgotten the parents in the command wework.salesSummary() so it did not work.

Thanks for the replies 🤓

   

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.