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?
我有一个全局函数来记录消息,还有一个函数名应该调用全局函数的类。诀窍是使用模块名(通常是xcode项目名或目标名)。但是,如果sourcefile是多个目标的一部分,那么应该怎么做呢?
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)
}
1 个解决方案
#1
1
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,你将能够实现它们:
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:
这太疯狂了,Swift中的字符串不能打印。如果你想自己添加,可以这样做:
extension String: Printable {
public var description: String { return self }
}
In the mean time, time to file a radar!
与此同时,是时候提交雷达了!
#1
1
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,你将能够实现它们:
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:
这太疯狂了,Swift中的字符串不能打印。如果你想自己添加,可以这样做:
extension String: Printable {
public var description: String { return self }
}
In the mean time, time to file a radar!
与此同时,是时候提交雷达了!