nonopolarity
nonopolarity

Reputation: 151076

Can we extend a class in playground on Xcode using Swift?

The following code runs perfectly if it is in a standalone command line app:

extension Int {
    func sayHello() {
    println("Hello, I'm \(self)")
    }
}

1.sayHello() 
2.sayHello()

However, in playground, it won't run, and the error is "(2 times)". Can we not extend a class in playground or how do we do it?

Upvotes: 2

Views: 704

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66282

A few corrections:

  • "(2 times)" is not an error. It means your code was executed two times.
  • Int is a struct (value type), not a class

You can click the eye icon to see the output:

Upvotes: 1

Related Questions