CocoaUser CocoaUser - 2 months ago 25x
Swift Question

How to call c TIFFGetField function in Swift?

How to call c function "TIFFGetField" in swift.

Does anyone have an idea or give me a sample? Thanks

//The function declared
extern int TIFFGetField(TIFF* tif, uint32 tag, ...);

/* sample: in objective-c is working very well */
unsigned int width;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);

/* sample: in swift failed */
var width: UInt32 = 0
TIFFGetField(tiff, uint32(TIFFTAG_IMAGEWIDTH), &width)

/* error message:
* 'TIFFGetField' is unavailable: Variadic function is unavailable


As noted in my comment, C-based variadic functions are not available in Swift. A good alternative in your case is using TIFFVGetField.

Just seeing the function signature and man docs, you may need to write something like this:

var width: UInt32 = 0
withUnsafeMutablePointer(&width) {widthPtr in
    let args: [CVarArgType] = [widthPtr]
    TIFFVGetField(tiff, UInt32(TIFFTAG_IMAGEWIDTH), getVaList(args))

First create an Array of CVarArgType, and then convert it to a CVaListPointer with the global function getVaList(_:).