Brandon Mahoney Brandon Mahoney - 3 months ago 33
Swift Question

No '|' candidates produce the expected contextual result type 'CGGradientDrawingOptions'

Going back through an old project I was going to add some features and ran into this error. Does anyone know how to eliminate this? It happened for the code below:

UInt32(CGGradientDrawingOptions.DrawsBeforeStartLocation) | UInt32(CGGradientDrawingOptions.DrawsAfterEndLocation))


Below is the full file:

import UIKit

class BackgroundView: UIView {

override func drawRect(rect: CGRect) {

// Background View

//// Color Declarations
var lightPurple: UIColor = UIColor(red: 0.377, green: 0.075, blue: 0.778, alpha: 1.000)
var darkPurple: UIColor = UIColor(red: 0.060, green: 0.036, blue: 0.202, alpha: 1.000)

let context = UIGraphicsGetCurrentContext()

//// Gradient Declarations
let purpleGradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), [lightPurple.CGColor, darkPurple.CGColor], [0, 1])

//// Background Drawing
let backgroundPath = UIBezierPath(rect: CGRectMake(0, 0, self.frame.width, self.frame.height))
CGContextSaveGState(context)
backgroundPath.addClip()
CGContextDrawLinearGradient(context, purpleGradient,
CGPointMake(160, 0),
CGPointMake(160, 568),
UInt32(CGGradientDrawingOptions.DrawsBeforeStartLocation) | UInt32(CGGradientDrawingOptions.DrawsAfterEndLocation))
CGContextRestoreGState(context)

}
}

Answer

CGGradientDrawingOptions since Swift 2.0 implements OptionSetType. This means that | operator is no longer necessary and you can write it like this:

let options: CGGradientDrawingOptions = [.DrawsBeforeStartLocation, .DrawsAfterEndLocation]

To get more info you can go here