Logging Method Signature Using Swift

Logging Method signature using swift

Check out a new library I've just published: https://github.com/DaveWoodCom/XCGLogger

It's a debug logging library for Swift.

The key to being able to use the #function macros, is to set them as default values to your logging function. The compiler will then fill them in using the expected values.

func log(logMessage: String, functionName: String = #function) {
print("\(functionName): \(logMessage)")
}

Then just call:

log("my message")

And it works as expected giving you something like:

whateverFunction(): my message

More info on how this works: https://www.cerebralgardens.com/blog/entry/2014/06/09/the-first-essential-swift-3rd-party-library-to-include-in-your-project

How to tell Swift which function signature to use between default optionals

Quick Answer for your issue: Xcode BUG

First of all, as your screenshots show, it's a bug. (try removing driven data or restarting stuff to make it work). The following is the answer to your question.



Explanation: How to tell the compiler to use which method signature

Swift infers the function signature based on the used Types in the expression. So imagine this:

class ViewController { }
class UIViewController { }

func instantiateInitialViewController() -> UIViewController? {}
func instantiateInitialViewController() -> ViewController? {}

So calling this would be ambiguous use of instantiateInitialViewController():

let theController = instantiateInitialViewController()

The solution for this is to tell the compiler what is the expected type explicitly:

let theController: ViewController? = instantiateInitialViewController()
let theOtherController: UIViewController? = instantiateInitialViewController()

The new instantiateInitialViewController return type is a protocol (ViewController) that constrained to UIViewController. So it should infer the type and have no logical difference from the old one. So just keep calm and just let it infer ;)

Swift: how to convert argument to argument name string?

Hm, if you called next method inside your function, it shows method declaration with all parameters:

func testFunction(param1: String, param2: String) {    
print(#function)
}

testFunction(param1:param2:)

How to print out the method name and line number in swift

Literal        Type     Value

#file String The name of the file in which it appears.
#line Int The line number on which it appears.
#column Int The column number in which it begins.
#function String The name of the declaration in which it appears.
#dsohandle UnsafeMutablePointer The dso handle.

Example

print("Function: \(#function), line: \(#line)") 

With default values in parameters you can also create a function

public func track(_ message: String, file: String = #file, function: String = #function, line: Int = #line ) { 
print("\(message) called from \(function) \(file):\(line)")
}

which can be used like this

track("enters app")

In Swift 2.1

 Literal        Type     Value

__FILE__ String The name of the file in which it appears.
__LINE__ Int The line number on which it appears.
__COLUMN__ Int The column number in which it begins.
__FUNCTION__ String The name of the declaration in which it appears.

for more info see the documentation

What is the method signature when it uses an external param name in Swift

It is timerMethodWithExternal: you can test that with object_getClass(t).instancesRespondToSelector(Selector("timerMethodWithExternal:"))

i used following code to introspect

func with(t: Test, inout count : CUnsignedInt) -> UnsafePointer<Method> {
var mc : CUnsignedInt = 0
return class_copyMethodList(object_getClass(t), &count)
}

var i=0
var mc : CUnsignedInt = 0
var t = Test()
var mlist = with(t,&mc)
var n : Int = Int(mc)
for (i=0; i<n;i++) {
println(sel_getName(method_getName(mlist[i])))
}

Swift function scope - referencing self (to mean self as the enveloping function)

If you want to print the name of the function, you can use __FUNCTION__

print(__FUNCTION__)

in your print statement, otherwise just use sampleFunction as an argument .

How to view method signatures when Objc-C is bridged to Swift and vice versa?

To see how an Objective-C interface is imported into Swift, select the header file and choose "Generated Interface" from the "Related Items" button:

Sample Image

Example: OClass.h:

NS_ASSUME_NONNULL_BEGIN

@interface OClass : NSObject

- (void)bar:(NSString *)string;

@end

NS_ASSUME_NONNULL_END

Generated interface:

open class OClass : NSObject {
open func bar(_ string: String)
}

To see how a Swift class is mapped to Objective-C, select a Swift file and choose "Generated Interface -> "ProjectName.h" from the "Related Items" button:

Sample Image

Example: "SClass.swift"

class SClass: NSObject {
@objc func foo() {

}
}

"MyProject-Swift.h":

// ...
SWIFT_CLASS("_TtC9MyProject6SClass")
@interface SClass : NSObject
- (void)foo;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end
// ...


Related Topics



Leave a reply



Submit