C# String interpolation in NSLog cause compile warning on iOS


(James Esword) #1
// Compile warning NW3: Format string should be a literal
NSLog($"a={1+1}");

Could this warning be avoided?


(marc hoffman) #2

Yes, by not misusing NSLog ;). NSLog requires a constant string, because otherwise you will run into bad trouble if the final a string contains a %. consider:

var X = "50% off";
NSLog($"buy now, {X}") // oops.

(James Esword) #3

Thanks marc!

So the best practise should be:

var x = "50% off";
NSLog("%@", $"buy now, {x}");

(marc hoffman) #4

yes, correct.