如何在SWIFT中访问UIColor的扩展? [英] How to access extension of UIColor in SWIFT?
问题描述
我对swift很新,并尝试创建UIColor类的扩展名为
i am very new to swift and trying to create an extension of UIColor class as
extension UIColor{
func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
在此之后,我将方法视为
After this i accessed the method as
btnShare.setTitleColor(UIColor.getCustomBlueColor(**UIColor**), forState: UIControlState.Normal)
我不知道我应该作为这个陈述的参数传递。
I dont know what I should pass as an argument to this statement.
推荐答案
你已经定义了一个实例方法 ,这意味着您只能在 UIColor
实例上调用
:
You have defined an instance method, which means that you can call
it only on an UIColor
instance:
let col = UIColor().getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState: .Normal)
编译器错误缺少参数的发生是因为
实例方法是Swift中的Curried函数,
所以我t可以等效地被称为
The compiler error "missing argument" occurs because Instance Methods are Curried Functions in Swift, so it could equivalently be called as
let col = UIColor.getCustomBlueColor(UIColor())()
(但这样做会很奇怪,而且我只将它添加到
解释错误信息的来源来自。)
(But that would be a strange thing to do, and I have added it only to explain where the error message comes from.)
但你真正想要的是类型方法( class func
)
extension UIColor{
class func getCustomBlueColor() -> UIColor{
return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00)
}
}
被称为
let col = UIColor.getCustomBlueColor()
// or in your case:
btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal)
,无需先创建 UIColor
实例。
这篇关于如何在SWIFT中访问UIColor的扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!