Source code to NSStringFromSelector() available anywhere?

Does anyone know if any Apple Open Source repository would contain the source code to NSStringFromSelector? TSAN has pointed out an issue in my code that stems from assuming that the value returned by NSStringFromSelector is immutable, and that the function itself is re-entrant. I’d like to check both those assumptions against the actual sources... assuming they haven't changed in macOS Sequoia to begin with.

Answered by DTS Engineer in 798379022

AFAIK NSStringFromSelector isn’t open source but it’s not a very complicated routine. What heavy lifting is being done is done by sel_getName, which is in the Darwin open source. Beyond that, the logic looks like this:

  1. If sel is nil return nil.

  2. If sel_getName returns NULL, return nil.

  3. Otherwise create a string for the C string using -initWithBytesNoCopy:length:encoding:freeWhenDone:.

Ah, and the NSStringFromSelector code hasn’t changed in macOS 15 beta, although it’s possible that sel_getName might’ve.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

AFAIK NSStringFromSelector isn’t open source but it’s not a very complicated routine. What heavy lifting is being done is done by sel_getName, which is in the Darwin open source. Beyond that, the logic looks like this:

  1. If sel is nil return nil.

  2. If sel_getName returns NULL, return nil.

  3. Otherwise create a string for the C string using -initWithBytesNoCopy:length:encoding:freeWhenDone:.

Ah, and the NSStringFromSelector code hasn’t changed in macOS 15 beta, although it’s possible that sel_getName might’ve.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Source code to NSStringFromSelector() available anywhere?
 
 
Q