0

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);

}

user3517759
  • 5
  • 1
  • 3
  • If you jump to the definition of `CGGradientDrawingOptions` in Xcode (Cmd + click), you see that it is an enum with values `CGGradientDrawingOptions .DrawsBeforeStartLocation` and `CGGradientDrawingOptions .DrawsAfterEndLocation` with an initializer from Int32. – Kevin Mar 31 '16 at 03:50

1 Answers1

0

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, [])
Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • Thanks a lot Scott it worked! btw @Kevin I did read through the definition of the function and tried using this ` CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, [4,5])` but it still threw the same error. So I was baffled and reverted back to a non-array int. – user3517759 Mar 31 '16 at 05:12
  • Enums in Swift are not (generally speaking) just a weird way of specifying integers like the are in C or Objective-C. They are different beast which makes them more powerful, and more safe, but it trips up "old-timers" who are used to the C way of doing things. :-) – Scott Thompson Mar 31 '16 at 05:16
  • Ha I will have to get used to this :) – user3517759 Mar 31 '16 at 05:21