Swift exercise "Windowing System": cannot create a new window

If I create and populate the mainWindow instance of Window I get the “expressions are not allowed at the top level” compile error because it seems that Swift doesn’t accept expressions outside function and methods unless it’s in the main file but if I remove the statements needed to create and configure the new window I get the “cannot find ‘mainWindow’ in scope” compile error.

I’m stuck and I’m not sure if there’s something I don’t understood or its a bug in the exercise.

import Foundation

struct Size {
    var width = 80
    var height = 60

    mutating func resize(newWidth: Int, newHeight: Int) {
        width = newWidth
        height = newHeight
    }
}

struct Position {
    var x = 0
    var y = 0

    mutating func moveTo(newX: Int, newY: Int) {
        x = newX
        y = newY
    }
}

class Window {
    var title = "New Window"
    let screenSize = Size(width: 800, height: 600)
    var size = Size()
    var position = Position()
    var contents: String? = nil

    func resize(to:Size) {
        size.width = max(min(to.width, screenSize.width - position.x), 1)
        size.width = max(min(to.height, screenSize.height - position.y), 1)
    }
    func move(to:Position) {
        position.x = max(min(to.x, screenSize.width - size.width), 0)
        position.y = max(min(to.y, screenSize.height - size.height), 0)
    }

    func update(title:String) {
        self.title = title
    }
    func update(text:String) {
        self.contents = text
    }

    func display() -> String {
        return "\(title)\nPosition: (\(position.x), \(position.y)), Size: (\(size.width) x \(size.height))\n\(contents ?? "[This window intentionally left blank]")\n"
    }
}

var mainWindow = Window()
mainWindow.update(title: "Main Window")
mainWindow.resize(to: Size(width: 400, height: 300))
mainWindow.move(to: Position(x: 100, y: 100))
mainWindow.update(text: "This is the main window")
2 Likes

Hello, I, like you, ran into the same problem with task 7 of the “Window System” exercise. My way of solving it was somewhat redundant, but basically I created a function that returned a Window() type object where I performed the instructions that the task asked me to later declare a variable called “mainWindow” as requested in the task and assign it the function that I previously created that returned the initialized Window() object. Here I show you my code:

func newWindow() -> Window {
	var window = Window()
	window.title = "Main Window"
	window.resize(to: Size(width: 400, height: 300))
	window.move(to: Position(x: 100, y: 100))
	window.update(text: "This is the main window")

	return window
}

let mainWindow = newWindow()

Upon completing the exercise, I reviewed someone else’s code to find out how they solved the exercise in this task and noticed that a user had solved it using a “closure” to initialize an instance of the Window class. I honestly don’t know about the subject. I just managed to solve it yesterday after researching this problem in this forum and seeing that only you published it, it motivated me to write this answer for possible people who may go through this same problem. This is the code that was used to solve this exercise using a “closure”:

let mainWindow: Window = {

	var window = Window()
	window.title = "Main Window"
	window.resize(to: Size(width: 400, height: 300))
	window.move(to: Position(x: 100, y: 100))
	window.update(text: "This is the main window")

	return window
}()

I will try to propose adding the subject of “closures” in the path of learning the Swift programming language so that when entering the topic of structures and classes, you can learn about the topic of closures at the same time and be able to do the “Windowing System” exercise in a simpler way for people who do not know this way of solving it, I hope my contribution to the forum is useful.