UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

2      

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()'

2      

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.

2      

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

Thanks for the replies 🤓

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.