Brandon Mahoney Brandon Mahoney - 1 year ago 131
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))
CGContextDrawLinearGradient(context, purpleGradient,
CGPointMake(160, 0),
CGPointMake(160, 568),
UInt32(CGGradientDrawingOptions.DrawsBeforeStartLocation) | UInt32(CGGradientDrawingOptions.DrawsAfterEndLocation))


Answer Source

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

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