Print Class Name of Current File in Swift

I’m trying to print the name of the current class in Swift. More specifically, I’d like achieve the following output:

myFunction() in ClassContainingTheFunction

I have the function name printing fine. This is the closest I’ve came to printing the class name along with the function name:

  • RPScreenRecorder stopRecording block not getting called
  • UITableView displays separator at wrong position in iOS8 for some cells
  • How to swipe right to show new View Controller in Swift 3?
  • Error: “No devices registered in member center” - Invalid UDID?
  • Updating a Table View From a Different File - Cannot invoke 'reloadData' with no arguments
  • Implement protocol through extension
  • println("\(__FUNCTION__) in \(__FILE__)") 
    

    prints

    myFunction() in path/to/TheFile.swift
    

    and

     println("\(__FUNCTION__) in \(object_getClassName(self))")
    

    prints

     myFunction() in [mangled class name here]
    

    Both of these are close to what I want, but the path is long, and the mangled class name can be very unreadable compared to the name I see in code.

    5 Solutions Collect From Internet About “Print Class Name of Current File in Swift”

    So your problem is only the long path for the shown String.
    My best idea would be to shorten the String to the filename only.

    var test: String = "/Users/user/Project/classfile.m"
    
    test = test.lastPathComponent // here it is only classfile.m
    test = test.stringByDeletingPathExtension // here it is only classfile
    

    Since object_getClassNAme(...) will use the real used class and not the name you used like the the class cluster, it is the wrong way for you. Learning the concrete class implementation names would make it easier though.


    Instead of test you use your way of getting the filename, like __FILE__. The following code produces the output you are looking for:

    println("\(__FUNCTION__) in \(__FILE__.lastPathComponent.stringByDeletingPathExtension)") 
    

    Swift 2.2

    In Swift 2.2 those old identifiers are deprecated and replaced with #file, #line, #column, and #function.

    print("\(#function) in \(#file.components(separatedBy: "/").last ?? "")")
    

    Output example:

    file81.swift
    

    My short answer is,

    print(" Function name : \(#function), Class Name : \(self.dynamicType), File Path : \(#file)")
    

    I wanted a solution that logged out a nice neat string like this (Swift 3.0) – Class Name – Function Name. eg :

    OnboardingGetStartedController – viewDidLoad()

    So I ended up with a short function that Ive put in my utils class, like this :

    class func logCurrentFunc(fileStr: String, funcStr: String) {
    
        var fileName = fileStr.components(separatedBy: "/").last ?? ""
        fileName = fileName.components(separatedBy:".").first ?? ""
        let printFunc = "\(fileName) - \(funcStr)"
        print(printFunc)
    }
    

    And I call this from anywhere in the app, like this :

    Utils.logCurrentFunc(#file, funcStr: #function)
    

    Its just a bit neater looking than other suggestions.

    In swift 3 it is now #file and #function

    Hope that helps.

    I think the following is what you want (in Swift 2.1 and up):

    print("\(__FUNCTION__) in \(self.dynamicType)")