How to Handle NULL in Swift?

So I was exploring Core Graphics and found several documents related to CGColorSpace are saying "If unsuccessful, returns NULL."

I know this NULL is from Objective-C or C, but how should they be handled in Swift? Is there any workaround or should I assume these functions always success in Swift?

Answered by OOPer in 681118022

An interesting example.

As you already know, a value that can be NULL would usually be imported into Swift as Optional and you use nil in Swift.

But in the example you have referred, the Swift version of the function signature shows the return value as non-Optional:

func CGColorSpaceCreateDeviceRGB() -> CGColorSpace

Interestingly, the original C-declaration in CGColorSpace.h is marked with cg_nullable:

/* Create a DeviceRGB color space. */

CG_EXTERN CGColorSpaceRef cg_nullable CGColorSpaceCreateDeviceRGB(void)
  CG_AVAILABLE_STARTING(10.0, 2.0);

You can find some functions in CGColorSpace.h are marked with cg_nullable and imported as non-Optional in Swift, and some others marked with __nullable (which is a standard way to tell it is nullable to Swift importer) and imported as Optional.

I'm not sure if this usage was intentional to distinguish nullable, but should be imported as Optional into Swift cases and simply nullable.


But, in fact, when I use CGColorSpaceCreateDeviceRGB() in Swift, I do expect this would never return nil.

When some functions may return NULL, but only in cases where continuing execution is not practical, such as Out of Memory, the imported type might be non-Optional in Swift.

It would be better if this sort of mismatching was clearly documented. You can send a bug report using Apple's Feedback Assistant.

In Swift, generally use nil instead of NULL.

Accepted Answer

An interesting example.

As you already know, a value that can be NULL would usually be imported into Swift as Optional and you use nil in Swift.

But in the example you have referred, the Swift version of the function signature shows the return value as non-Optional:

func CGColorSpaceCreateDeviceRGB() -> CGColorSpace

Interestingly, the original C-declaration in CGColorSpace.h is marked with cg_nullable:

/* Create a DeviceRGB color space. */

CG_EXTERN CGColorSpaceRef cg_nullable CGColorSpaceCreateDeviceRGB(void)
  CG_AVAILABLE_STARTING(10.0, 2.0);

You can find some functions in CGColorSpace.h are marked with cg_nullable and imported as non-Optional in Swift, and some others marked with __nullable (which is a standard way to tell it is nullable to Swift importer) and imported as Optional.

I'm not sure if this usage was intentional to distinguish nullable, but should be imported as Optional into Swift cases and simply nullable.


But, in fact, when I use CGColorSpaceCreateDeviceRGB() in Swift, I do expect this would never return nil.

When some functions may return NULL, but only in cases where continuing execution is not practical, such as Out of Memory, the imported type might be non-Optional in Swift.

It would be better if this sort of mismatching was clearly documented. You can send a bug report using Apple's Feedback Assistant.

How to Handle NULL in Swift?
 
 
Q