println on Swift through the terminal

3

I'm trying to compile a people.swift class with Terminal of MacOSX

class People
{
    let name:String = ""
    let age:Int = 0

    init(name:String, age:Int)
    {
        self.name = name
        self.age = age
    }

    let anyPeople = People.init(name:"Jaum", age:23)
    println("O \(anyPeople.name) tem \(anyPeople.age) anos.")
}

The command I am giving is ./swift -emit-executable people.swift

But you're giving the following error

people.swift:13:5: error: expected declaration
    println("O \(anyPeople.name) tem \(anyPeople.age) anos.")
    ^
    
asked by anonymous 06.06.2014 / 21:53

1 answer

6

A class is not a procedural structure and can not contain statements of execution instructions unless they are within a method.

The last two lines of the code you posted refer to the use of the class you are defining. In the XCode Playground you can successfully execute your code if you put them out of the class:

class People {
   ...

}

let anyPeople = People.init(name:"Jaum", age:23)
println("O \(anyPeople.name) tem \(anyPeople.age) anos.")

If you put all this within a People.swift file, and you have xcrun set to XCode6, you can run it on the Terminal using:

xcrun swift -i People.swift

that will print:

O Jaum tem 23 anos.

If it does not work, your XCode Tools may not be selected for XCode 6. You can select it using:

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

or

sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer

If you are using XCode Beta.

(source: link )

    
06.06.2014 / 22:28