How to access extension of UIColor in SWIFT?
How to access extension of UIColor in SWIFT?
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.
Answer by RoNiT for How to access extension of UIColor in SWIFT?
You just need to change your statement like,
btnShare.setTitleColor(UIColor().getCustomBlueColor(), forState:.Normal) More detailed explanation is here.
Answer by Martin R for How to access extension of UIColor in SWIFT?
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) 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.)
But what you really want is a type method (class func)
extension UIColor{ class func getCustomBlueColor() -> UIColor{ return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } } which is called as
let col = UIColor.getCustomBlueColor() // or in your case: btnShare.setTitleColor(UIColor.getCustomBlueColor(), forState: .Normal) without the need to create an UIColor instance first.
Answer by Chuck for How to access extension of UIColor in SWIFT?
You defined a instance function. It means you need an instance of UIColor in case to use getCustomBlueColor()-method.
It looks like you want to have a class method, instead of the instance method. So you have to change your definition like this:
extension UIColor{ class func getCustomBlueColor() -> UIColor{ return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } } Note the 'class' before func, so the method is now accessible as a class method.
The same story using class methods in a structure:
struct MyColors{ static func getCustomBlueColor() -> UIColor{ return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } } let color = MyColors.getCustomBlueColor() If you just want to have a class with some color definitions, I recommend you to use a struct over a class or extension:
struct MyColors{ static var getCustomBlueColor = { return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } } let color = MyColors.getCustomBlueColor() Answer by Eric Hodgins for How to access extension of UIColor in SWIFT?
Could use a computed property:
extension UIColor { var customBlueColor: UIColor { return UIColor(red:0.043, green:0.576 ,blue:0.588 , alpha:1.00) } } And then to call it:
UIColor().customBlueColor Answer by Gene Loparco for How to access extension of UIColor in SWIFT?
I don't know why Eric Hodgins was down voted, as his answer is the most "Swift 3 like" answer of the bunch.
With Swift 3, predefined UIColors are used accordingly:
var myColor: UIColor = .white // or .clear or whatever Therefore, if you want something similar, such as the following...
var myColor: UIColor = .myCustomColor ...then, you would define the extension like so:
extension UIColor { public class var myCustomColor: UIColor { return UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0) } } In fact, Apple defines white as:
public class var white: UIColor Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:
Post a Comment