idelara
idelara

Reputation: 1816

The UIColor Class does not let me instantiate a UIColor. Has to do with the ? ! symbols

I am trying to create an instance of an UIColor with the following code:

var someColor:UIColor = UIColor.colorWithAlphaComponent(0.3)

The precompiler tells me that there is an error. It says that UIColor! is not convertible to UIColor.

This is one of Swift's biggest mysteries I have not solved.

What does UIColor! mean? More in detail, I do not get why is the exclamation ! sign is there.

I am kinda familiar with ?. What I understand is that it is an optional. If it exists it holds its' value, and if it does not, it is set to NIL.

Any advice and correction is very much appreciated.

Thank you for solving my question.

Cheers!

Upvotes: 1

Views: 1284

Answers (2)

codester
codester

Reputation: 37189

Check the documentation of UIColor for colorWithAlphaComponent it is instance method.So you need to you need to call this on UIColor instance.

Upvotes: 0

jrturton
jrturton

Reputation: 119242

The method you're using is an instance method of UIColour, not a class method. Do you mean this instead:

UIColor(white:1.0, alpha:0.3)

The method you're using should be called on an existing colour. It's not an initialiser. The error message isn't very helpful, I agree. I think this:

It says that UIColor! is not convertible to UIColor.

Is Swift attempting to convert the class into an instance.

Upvotes: 3

Related Questions