Antzi
Antzi

Reputation: 13414

Extra argument in call when using var

I'm trying to use this code:

var alpha : Float
alpha = 0.5
self.view.backgroundColor = UIColor(red: 1, green:0, blue: 0, alpha:alpha)

However, I get the error:

Extra argument 'green' in call

What is wrong with this code? Moreover, why is

self.view.backgroundColor = UIColor(red: 1, green:0, blue: 0, alpha: 0)

working just fine?

Upvotes: 2

Views: 2199

Answers (5)

Dane Jordan
Dane Jordan

Reputation: 1139

My particular iteration of this error happened when I was trying to set the border color of a button, and was getting the "extra argument 'green' in call" error, but once I stored it in a constant the true error arose, which was the constant not being CGColor. So this fixed it.

  let borderColor:UIColor = UIColor(red: 23/255, green: 247/255, blue: 252/255, alpha: 1)
  loginButton.layer.borderColor = borderColor.CGColor

Upvotes: 0

Akshay Nalawade
Akshay Nalawade

Reputation: 1447

If you are using variables use following -

var color: UIColor = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))

Upvotes: 2

colinyoung
colinyoung

Reputation: 21

This also happens when you unwrap the a UIColor instance that wasn't declared as optional.

Instead of:

let brokenColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)!

Use this:

let color: UIColor! = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

Upvotes: 2

Antzi
Antzi

Reputation: 13414

Answer was: Swift UIColor initializer - compiler error only when targeting iPhone5s

Use float instead of integers.

UIColor(red: 1.0, green:0.0, blue: 0.0, alpha:alpha)

Upvotes: 3

GNewc
GNewc

Reputation: 445

Put a space after the semicolons in the call

green: 0, alpha: alpha

Upvotes: -6

Related Questions