iosswiftloggingnslogswift5

How to exclude project name from NSStringFromClass in Swift?


I am logging class names and enums in Swift project. I saw its appending project name as prefix in each of instances string description. For example,

I created extension of NSObject,

public extension NSObject {

    var classTag: String {
        return NSStringFromClass(type(of: self))
    }
}

I am using in in AppDelegate,

class AppDelegate: UIResponder, UIApplicationDelegate {

    func testFunc() {
        print(classTag, "Testing log")
    }
}

Its output is,

TestProject.AppDelegate Testing log

How can I remove TestProject. and print only AppDelegate?

NB: When I am printing or logging enums its behaving same (i.e. appending Project name automatically in description)


Solution

  • You can simply use String(describing:) instead of NSStringFromClass like so,

    public extension NSObject {
        var classTag: String {
            return String(describing: type(of: self)) //here....
        }
    }