7

Given the following two lines, XCode is showing an error for the second line saying Cannot convert value of type 'String' to expected argument type 'OSLogMessage'.

logger.info("Device found:")
logger.info(String(describing: device))

Can someone please explain why this error is shown? In both cases, the parameter is of type String. (I guess)

Currently, I fix this by using string interpolation. But this does not feel right. Is there a better way than:

logger.info("\(String(describing: device))")
Stephan
  • 1,791
  • 1
  • 27
  • 51
  • This https://stackoverflow.com/questions/62675874/xcode-12-and-oslog-os-log-wrapping-oslogmessage-causes-compile-error-argumen might be helful. – Kaiser Abliz Dec 16 '22 at 05:23
  • Thanks for sharing. What I get from this post is that interpolation seems to be something special in Swift? It says that OSLog received interpolation support. Coming from languages like TypeScript or C#, there interpolation is supported by any String. But in Swift it seems one has to enable this? So for me, a String is just a String. With or without interpolation. Wether created via "..." or String(...). This is what I don't understand. – Stephan Dec 16 '22 at 09:21

1 Answers1

1

The (surprising, perhaps) answer is that OSLog doesn't allow an arbitrary string to be logged for privacy reasons. It's not a language limitation but a policy to control the availability of user data in logs.

Nestor
  • 2,753
  • 2
  • 29
  • 34