Swift:确定哪个对象叫做函数?

时间:2022-07-24 16:04:50

I am writing some Swift code and I would like to know the class of the object that called the function. I don't want to pass in any parameters. From within the function I want to be able to know who called it.

我正在编写一些Swift代码,我想知道调用该函数的对象的类。我不想传递任何参数。从函数中我想知道是谁调用它。

Any suggestion?

有什么建议吗?

4 个解决方案

#1


19  

If you want to do that using swift, you can do this:

如果你想使用swift做到这一点,你可以这样做:

func debug(file: String = #file, line: Int = #line, function: String = #function) -> String {
    return "\(file):\(line) : \(function)"
}

It's quite simple. Ignore syntax highlight, it's wrong.

这很简单。忽略语法高亮,这是错误的。

#2


6  

To access the underlying class of a method from within itself, use the dynamicType property:

要从内部访问方法的基础类,请使用dynamicType属性:

 self.dynamicType

If you want to know the origin of the original call, you can use NSThread to return debugging information about the stack:

如果您想知道原始调用的来源,可以使用NSThread返回有关堆栈的调试信息:

 NSThread.callStackSymbols()

This method returns a descriptive array of values that you're used to seeing when exceptions are thrown. The strings represent a backtrace of all current activity on your call stack.

此方法返回一个描述性值数组,您可以在抛出异常时看到这些值。字符串表示调用堆栈上所有当前活动的回溯。

I don't want to be presumptuous, but it seems to me that outside of debugging, there isn't a good reason, conceptually, at least, to know the origin of a specific method call for any and every function. If you need to retrieve the class Type of the last method call on the stack, why not implement an interface that lets you access this information through a straightforward route?

我不想放肆,但在我看来,除了调试之外,从概念上来说,至少要知道任何和每个函数的特定方法调用的起源都没有充分的理由。如果需要检索堆栈上最后一个方法调用的类Type,为什么不实现一个允许您通过简单路径访问此信息的接口?

#3


6  

You can use following template to know from which file, line number in file, and function this someFunction is called:

您可以使用以下模板来了解文件中的哪个文件,行号以及调用someFunction的函数:

func someFunction(file: String = #file, line: Int = #line, function: String = #function)
{
    NSLog("\(file.lastPathComponent):\(line) : \(function)")
}

#4


1  

Swift 2.2:

Swift 2.2:

print("\(self.debugDescription) \(#line) \(#function)")

or add a (sender: Anyobject) as parameter to that function, and then print the sender (the function caller) like this:

或者将(sender:Anyobject)作为参数添加到该函数,然后打印发送者(函数调用者),如下所示:

func yourFunc(sender: AnyObject){
     print(sender)
}

#1


19  

If you want to do that using swift, you can do this:

如果你想使用swift做到这一点,你可以这样做:

func debug(file: String = #file, line: Int = #line, function: String = #function) -> String {
    return "\(file):\(line) : \(function)"
}

It's quite simple. Ignore syntax highlight, it's wrong.

这很简单。忽略语法高亮,这是错误的。

#2


6  

To access the underlying class of a method from within itself, use the dynamicType property:

要从内部访问方法的基础类,请使用dynamicType属性:

 self.dynamicType

If you want to know the origin of the original call, you can use NSThread to return debugging information about the stack:

如果您想知道原始调用的来源,可以使用NSThread返回有关堆栈的调试信息:

 NSThread.callStackSymbols()

This method returns a descriptive array of values that you're used to seeing when exceptions are thrown. The strings represent a backtrace of all current activity on your call stack.

此方法返回一个描述性值数组,您可以在抛出异常时看到这些值。字符串表示调用堆栈上所有当前活动的回溯。

I don't want to be presumptuous, but it seems to me that outside of debugging, there isn't a good reason, conceptually, at least, to know the origin of a specific method call for any and every function. If you need to retrieve the class Type of the last method call on the stack, why not implement an interface that lets you access this information through a straightforward route?

我不想放肆,但在我看来,除了调试之外,从概念上来说,至少要知道任何和每个函数的特定方法调用的起源都没有充分的理由。如果需要检索堆栈上最后一个方法调用的类Type,为什么不实现一个允许您通过简单路径访问此信息的接口?

#3


6  

You can use following template to know from which file, line number in file, and function this someFunction is called:

您可以使用以下模板来了解文件中的哪个文件,行号以及调用someFunction的函数:

func someFunction(file: String = #file, line: Int = #line, function: String = #function)
{
    NSLog("\(file.lastPathComponent):\(line) : \(function)")
}

#4


1  

Swift 2.2:

Swift 2.2:

print("\(self.debugDescription) \(#line) \(#function)")

or add a (sender: Anyobject) as parameter to that function, and then print the sender (the function caller) like this:

或者将(sender:Anyobject)作为参数添加到该函数,然后打印发送者(函数调用者),如下所示:

func yourFunc(sender: AnyObject){
     print(sender)
}