5

I want to put together a runnable script and a class to include from a separate module.

If I put the class into the script like this, it works.

#!/usr/bin/env swift
class HelloWorld {
    func greet() {
        print("Hello World")
    }
}
var h = HelloWorld()
h.greet()

However, when I put the HelloWorld class into a HelloWorld.swift module, I have found no solution to get it to work.

FunTimeCoding
  • 53
  • 1
  • 3

1 Answers1

2

You just need to do:

$ swift HelloWorld.swift

And you HelloWorld.swift:

class HelloWorld {
    func greet() {
        print("Hello World")
    }
}
var h = HelloWorld()
h.greet()

Important: as of Swift 1.2 you can only have one big scripting file. Reference

Community
  • 1
  • 1
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45