Search code examples
swiftcggradientref

cannot convert value of type int to expected argument type CGGradientDrawingOptions


I am new to Swift and I am trying to create a background gradient using CGGradient but I get the following error.

cannot convert value of type int to expected argument type CGGradientDrawingOptions

I do not get why I am not supposed to use an int here. Any help will be appreciated.

override func viewDidLoad() {
    super.viewDidLoad()
    var currentContext = UIGraphicsGetCurrentContext()
    CGContextSaveGState(currentContext);
    var colorSpace = CGColorSpaceCreateDeviceRGB()

    var topColor = UIColor(red: (57/255.0), green: (150/255.0), blue: (199/255.0), alpha: 1)
    var startColorComponents = CGColorGetComponents(topColor.CGColor)

    var middleColor = UIColor(red: (90/255.0), green: (60/255.0), blue: (117/255.0), alpha: 0.85)
    var middleColorComponents = CGColorGetComponents(middleColor.CGColor)

    var bottomColor = UIColor(red: (215/255.0), green: (109/255.0), blue: (109/255.0), alpha: 1)
    var endColorComponents = CGColorGetComponents(bottomColor.CGColor)

    var colorComponents
        = [startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3],
            middleColorComponents[0], middleColorComponents[1], middleColorComponents[2], middleColorComponents[3],
                endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3]
            ]

    var locations:[CGFloat] = [0.0, 0.35, 1.0]

    var gradient = CGGradientCreateWithColorComponents(colorSpace,&colorComponents,&locations,3)

    var startPoint = CGPointMake(-0.3, 0.1)
    var endPoint = CGPointMake(0.2, 1.3)

    CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, 0)

    CGContextRestoreGState(currentContext);

}


Solution

  • I assume you are getting the error on the line that reads:

    CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, 0)
    

    The problem here is your last parameter where you are passing 0 (an integer).

    The declaration for this routine is:

    public func CGContextDrawLinearGradient(c: CGContext?, 
        _ gradient: CGGradient?, 
        _ startPoint: CGPoint, 
        _ endPoint: CGPoint, 
        _ options: CGGradientDrawingOptions)
    

    In Objective-C the CGGradientDrawingOptions type is an integer-valued thing where 0 means no options. In Swift the drawing options are declared as an enum of the type OptionSetType. The compiler is complaining because it's expecting this enum and not an integer.

    The way you would specify those options in Swift looks like an array, or set containing elements from the enum. So if you wanted both the DrawsBeforeStartLocation option and the DrawsAfterEndLocation you would write:

    CGContextDrawLinearGradient(someContext, 
       <other arguments here>
        [.DrawsBeforeStartLocation, .DrawsAfterEndLocation])
    

    In your particular case, you don't want either option so just pass an empty option set:

     CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, [])