How to I get MacOS Build Number programmatically?

On MacOS, let's say I am using Big Sur Version 11.6 (20G165)

Is there any API on Xcode that would allow me to retrieve the build number programmatically, (in this case 20G165)?

For version, I can use [[NSProcessInfo processInfo] operatingSystemVersion], but can't seem to be able to find the API for the build number.

Thanks!

As far as I tried, operatingSystemVersionString returns a version string containing the build number:

(In Swift)

print(ProcessInfo.processInfo.operatingSystemVersionString)

Output (on my Mac):

Version 11.6 (Build 20G165)

I'm not sure if there is any API which returns only the build number.

The build string is available via sysctlbyname(3) (man page) for the name "kern.osversion".

How to I get MacOS Build Number programmatically?
 
 
Q