How to create a string with format?

Apurv picture Apurv · Jun 6, 2014 · Viewed 258.1k times · Source

I need to create a string with format which can convert int, long, double etc. types into string. Using Obj-C, I can do it via below way.

NSString *str = [NSString stringWithFormat:@"%d , %f, %ld, %@", INT_VALUE, FLOAT_VALUE, DOUBLE_VALUE, STRING_VALUE];

How to do same with swift?

Answer

realityone picture realityone · Jun 7, 2014

I think this could help you:

let timeNow = time(nil)
let aStr = String(format: "%@%x", "timeNow in hex: ", timeNow)
print(aStr)

Example result:

timeNow in hex: 5cdc9c8d