Linked Questions

Popular Questions

Automatic bridging of C struct functions to Swift methods

Asked by At

Is there a naming convention that will allow Swift to automatically associate C functions that act on a struct with the struct itself?

For instance, CGRect is declare as following:

struct CGRect {
    CGPoint origin;
    CGSize size;
};
typedef struct CG_BOXABLE CGRect CGRect;

...

CG_EXTERN CGFloat CGRectGetMinX(CGRect rect)
    CG_AVAILABLE_STARTING(10.0, 2.0);

But I can write Swift code like this:

let rect = CGRect()
let minX = rect.minX

I've attempted to copy the same in the hope that I could get the same behaviour from my own C structs, but can't seem to get it to work correctly.

Is this automatic behaviour due to exactly how the struct is declared, or is it added elsewhere via extensions / some other magic? If it's via extensions, how do you disallow the original CGRectGetMinX function in Swift?

Related Questions