swiftbuild-time

Get build date and time in Swift


I'm using __DATE__ and __TIME__ in Objective-C to get the build date and time of my app. I can't find a way to get this information in Swift. Is it possible?


Solution

  • You can use #line, #column, and #function.


    Original answer:

    Create a new Objective-C file in your project, and when Xcode asks, say yes to creating the bridging header.

    In this new Objective-C file, add the following the the .h file:

    NSString *compileDate();
    NSString *compileTime();
    

    And in the .m implement these functions:

    NSString *compileDate() {
        return [NSString stringWithUTF8String:__DATE__];
    }
    
    NSString *compileTime() {
        return [NSString stringWithUTF8String:__TIME__];
    }
    

    Now go to the bridging header and import the .h we created.

    Now back to any of your Swift files:

    println(compileDate() + ", " + compileTime())