user965972
user965972

Reputation: 2587

calling a global function from a local function with the same name in swift

I have a global function to log messages and a class with the same function name that should call the global function. The trick is to use the module name (typically the xcode project name or target name). But how are you supposed to do this if the sourcefile is part of multiple targets?

func look(){
   //log stuff
}

class MyClass
{
  func look(){
    TargetName.look()
  }
}

Also, why doesn't String conform to the Printable protocol? Seems like an odd choice because this won't work with a String:

func look(value : Printable?)
{
   println(value)
}

Upvotes: 3

Views: 2330

Answers (1)

Nate Cook
Nate Cook

Reputation: 93276

Well, if you can help it, don't do the first thing. If you must, make your global functions static methods of a struct and you'll be able to reach them that way:

struct Logger {
    static func look(){
        //log stuff
    }
}

class MyClass {
    func look(){
        Logger.look()
    }
}

That is crazy that String in Swift isn't Printable. If you want to add it yourself, this will do it:

extension String: Printable {
    public var description: String { return self }
}

In the mean time, time to file a radar!

Upvotes: 1

Related Questions