Reputation: 5593
I'm trying to run ./swift -emit-executable shape.swift
by terminal
shape.swift
class Shape
{
let name:String = ""
init(name:String)
{
self.name = name
}
let anyShape = Shape.init(name:"Jaum")
println("Name, \(anyShape.name).")
}
I'm getting this error:
shape.swift:11:5: error: expected declaration
println("Name, \(anyShape.name).")
^
What am I doing wrong?
Upvotes: 2
Views: 182
Reputation: 70098
You can't have declarations at the root level of a class, move them outside.
Don't use .init
to create a class instance.
Use print
instead of println
(it has changed in Swift 2).
And don't give a value of ""
to your immutablename
if you want to use the initializer, just declare the type.
class Shape {
let name:String
init(name:String) {
self.name = name
}
}
let anyShape = Shape(name:"Jaum")
print("Name, \(anyShape.name).")
Last note, it's not swift
but swiftc
which is able to create an executable:
swiftc -emit-executable shape.swift
Upvotes: 4