println on Swift via terminal

Asked

Viewed 215 times

3

I’m trying to compile a class people.swift for Terminal 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’m giving is ./swift -emit-executable people.swift

But the following mistake is making

people.swift:13:5: error: expected declaration
    println("O \(anyPeople.name) tem \(anyPeople.age) anos.")
    ^
  • 3

    I haven’t studied Swift yet (it’s here on my list!), but there may be code loose like this within the class?

  • 1

    IMHO [tag:Swift-language] seems terrible to me. Why not simply [tag:Swift]?

  • @helderdarocha I had created [tag:Swift] but after looking at Soen I saw that they use the language ending on that tag.

  • I’m back to the original tag that was created...

  • They use it because there were already questions about the other language 'Swift', which hardly anyone will use around here. There is also a wide discussion on the goal. The way it walks should be 'Swift' and 'Swift-language' as synonyms.

  • There’s a vote on the Soen goal. [tag:Swift] is winning: http://meta.stackoverflow.com/questions/259995/tag-name-poll-swift-apple-swift-swift-swift-language?cb=1

Show 1 more comment

1 answer

6


A class is not a procedural structure and cannot contain sequence of instructions for execution unless they are within a method.

The last two lines of 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 place them out of class:

class People {
   ...

}

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

If you put all this inside a file People.swift, and is with the xcrun configured for Xcode6, can run on Terminal using:

xcrun swift -i People.swift

that will print:

O Jaum tem 23 anos.

If it doesn’t work it may be that your Xcode Tools is not 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: https://twitter.com/clattner_llvm/status/474593140511211520)

  • That’s right! A little lack of attention from me and I didn’t see it pass.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.