Grimxn Grimxn - 1 year ago 216
Swift Question

CGImageDestinationCreateWithData constants in iOS

I have the following code which turns a


import Foundation
import CoreGraphics
import ImageIO
// ... snip ...
let data = NSMutableData()
if let dest = CGImageDestinationCreateWithData(data, kUTTypePNG, 1, nil), let image = self.backgroundImage {
CGImageDestinationAddImage(dest, image, nil)
if CGImageDestinationFinalize(dest) {
return data as Data
return nil

The code compiles fine in Mac-OS, but
is undefined in iOS. The actual value of the constant is
, and obviously, replacing the constant with that value allows iOS to compile the code fine.

But avoiding magic strings/numbers is the reason we use constants in the first place - is there an alternative constant in Swift-iOS?

Answer Source

From Mobile Core Services Framework in the "iOS Technology Overview":

The Mobile Core Services framework (MobileCoreServices.framework) defines the low-level types used in uniform type identifiers (UTIs).

For more information about the types defined by this framework, see Uniform Type Identifiers Reference.


import MobileCoreServices


public let kUTTypePNG: CFString

and other UTI constants available to your code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download