Search code examples
swiftcocoagame-developmentimporterror

How to export a swift class that I made from one file and import it to another using the cocoa framework without Xcode?


I am trying to make a game using cocoa framework in swift WITHOUT Xcode. I have made a file where I made a class playerCharacter and that I will add to later. playerCharacter.swift :

import Cocoa
import Quartz

public class playerCharacter: NSView {
    var x: Int
    var y: Int
    let color: NSColor

    init(x: Int, y: Int) {
        self.x = x
        self.y = y
        self.color = NSColor.darkGray

        let frame = NSRect(x: x, y: y, width: 50, height: 50)
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder: ) has not been implemented")
    }
    override public func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Draw the player character square with the specified color
        color.setFill()
        dirtyRect.fill()
    }
}

Then I have a file main.swift where I am calling the playerCharacter class and drawing it on to a window defined by the class Window main.swift :

`import Cocoa
import Quartz

func askUserForWidthAndHeight() -> (width: Int, height: Int) {
    print("What is the width of the window? We recommend 1160. You can always leave the app and reopen to change")
    var width = Int(readLine() ?? "") ?? 0
    print("What is the height of the window? We recommend 775. You can always leave the app and reopen to change")
    var height = Int(readLine() ?? "") ?? 0
    print("Window Created!")

    while width <= 0 || height <= 0 {
        print("The width and height must be positive numbers.")
        width = Int(readLine() ?? "") ?? 0
        height = Int(readLine() ?? "") ?? 0
    }
    return (width, height)
}

var (width, height) = askUserForWidthAndHeight()

class Window: NSWindow {
    convenience init() {
        self.init(contentRect: NSRect(x: 0, y: 0, width: width, height: height), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullScreen], backing: .buffered, defer: false)
    center()

    let player = playerCharacter(x: width/2, y: height/2)
    contentView?.addSubview(player)
    }
}

let window = Window()
window.makeKeyAndOrderFront(nil)
window.toggleFullScreen(nil)

let app = NSApplication.shared
app.setActivationPolicy(.regular)
app.run()`

But when I try to run I get the error that playerCharacter cannot be found in scope.

I have tried to export it from playerCharacter.swift like this :

public class playerCharacter: NSView {..}

then I tried import in these ways:

import playerCharacter
import playerCharacter.swift
import class playerCharacter
import playerCharacter from playerCharacter.swift

I also tried no import

How should I import/export a class from one file to another?


Solution

  • To combine the two files above without using Xcode try the following:

    1. Create two files: name the first class file 'player.swift'. Name the second file 'main.swift'.
    2. Place the two files in a project folder called 'PlayerCharacter'.
    3. Migrate to the project folder using Terminal.
    4. On the command line enter this code: swiftc player.swift main.swift -o playerDemo and hit enter.
    5. In a separate Terminal window you should be prompted to set the width and height for the main window.
    6. After entering these two numbers you should see the window with a small dark gray rectangle in its center.

    You don't need to 'import' anything extra (other than Cocoa). It should automatically find your class. You also don't really need 'import Quartz' unless you need it for something else that you haven't shown us.

    The command line creates an executable file only. I presume you already have some means of creating an app other than Xcode.