本文介绍了从AnyObject投射到CGColor?没有错误或警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好StackOverflow:)

Hello StackOverflow :)

自从我升级到Swift 2.0以来,我一直遇到一个奇怪的问题

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

我正在尝试设置边框颜色,所以我在写self.layer.borderColor = borderColor as! CGColor,其中borderColor是AnyObject,而self.layer.borderColor是CGColor?

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?

如果我写self.layer.borderColor = borderColor as! CGColor我会收到警告

,建议改为使用as?

如果我改写self.layer.borderColor = borderColor as? CGColor,我会收到错误消息

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

只是为了确保我没有丢失任何东西,我还尝试编写container.layer.borderColor = borderColor as CGColorcontainer.layer.borderColor = borderColor这两个都给出了以下错误:

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:

仅在使用as!时运行XCode给出的警告并不是那么糟糕,但是我希望保持我的代码不受警告.因此,我真的需要您的帮助.这是我不了解的事情,还是我应该报告的Swift 2.0中的错误.

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.

干杯!

Jacob

推荐答案

xcode还给了我以下提示:

xcode also gives me the following hint:

好吧...当xcode这么说的时候...

well... when xcode says so...

layer.borderColor = (borderColor as! CGColor)

这篇关于从AnyObject投射到CGColor?没有错误或警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:57