Casting from AnyObject to CGColor? without errors or warnings

Casting from AnyObject to CGColor? without errors or warnings

Hello StackOverflow :)

I've been running into a weird problem since I upgraded to swift 2.0

I'm trying to set a border color, so I'm writing self.layer.borderColor = borderColor as! CGColor where borderColor is an AnyObject, while self.layer.borderColor is a CGColor?

If I write self.layer.borderColor = borderColor as! CGColor I get the warning

Treating a forced downcast to 'CGColor' as optional will never produce 'nil'

and is recommended to instead use as?

If I instead write self.layer.borderColor = borderColor as? CGColor I get the error

Conditional downcast to CoreFoundation type 'CGColor' will always succeed

Just to make sure I wasn't missing something I also tried writing container.layer.borderColor = borderColor as CGColor and container.layer.borderColor = borderColorBoth of these gave the following error:

'AnyObject' is not convertible to 'CGColor'; did you mean to use 'as!' to force downcast?

Just running with the warning given by XCode when using as! is not all that terrible, but I would prefer to keep my code warning free. To avhieve that I really need your help SO. Is this something I'm not understanding or is it simply a bug in Swift 2.0 that I should instead report.

Cheers!

Jacob


xcode also gives me the following hint:

Add parentheses around the cast to silence this warning

well... when xcode says so...

layer.borderColor = (borderColor as! CGColor)

ios: Trouble retrieving a CGColor from a Swift dictionary, I have no issue creating the dictionary and storing the CGColor I did find Casting from AnyObject to CGColor? without errors or warnings  9 Casting from AnyObject to CGColor? without errors or warnings Nov 7 '15 7 Swift - How to convert String to Double Jul 15 '15 7 Global constants file in Swift Jul 18 '15


You are using layer color, which is core graphics, you need to set core graphic color with property cgColor

layer.borderColor = UIColor.red.cgColor

unexpectedly found nil while unwrapping an Optional value from , When I run this function I get a fatal error. fatal error: unexpectedly found nil while I did find Casting from AnyObject to CGColor? without errors or warnings  CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).


Yes, it is a well-know bug and it is still unresolved! Unnecessary message for non-optional to non-optional cast

If you don't like the warning you should follow the Xcode advice:

Add parentheses around the cast to silence this warning.

Trouble retrieving a CGColor from a Swift dictionary, error: conditional downcast to CoreFoundation type 'CGColor' will I did find Casting from AnyObject to CGColor? without errors or warnings  Please note that all Coyote Graphics routines use cgColor internally to specify their colors in a color-model independent way. It is not necessary to call cgColor yourself unless you are using it with a traditional IDL command (e.g., Plot).


Type Casting, Type casting in Swift is implemented with the is and as operators. These two This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type. AnyObject can represent an instance of any class type. Use Any and Warning; things.append(optionalNumber as Any) // No warning. Error  The compiler is warning you of an unchecked cast, not an unnecessary cast. This warning seems perfectly valid. You are downcasting an Object reference to a T reference. In principle this is unsafe, hence the warning, though in this particular case we know that the reference is null, and thus the downcast is safe.


User André Slotta, Casting from AnyObject to CGColor? without errors or warnings. Hello StackOverflow :) I've been running into a weird problem since I upgraded to swift 2.0 I'm  The result of session.getAttribute() is an object which could be anything, but since you “know” it’s a HashMap<String, String> you’re just casting without checking it first. Thus, the warning. To be pedantic, which Java wants you to be in this case, you should retrieve the result and verify it’s compatibility with instanceof.


Swift Blog, Top Questions. No questions with score of 5 or more. Top Answers Casting from AnyObject to CGColor? without errors or warnings · stackoverflow.com. A. If you compile an interface without errors, a .class file is created for the interface. B. If you compile a class without errors but with warnings, a .class file is created. D. If you compile an interface without errors, but with warnings, a .class file is created for the interface.