How to accomplish this similar code with swift. However when I tried to accomplish with the swift, it is giving an error Extra argument green
in call
+(UIColor *)colorWithValues:(NSString *)colorValues{
NSArray *colorComponents = [colorValues componentsSeparatedByString:@","];
CGFloat red = [[colorComponents objectAtIndex:0] floatValue]/255.0;
CGFloat green = [[colorComponents objectAtIndex:1] floatValue]/255.0;
CGFloat blue = [[colorComponents objectAtIndex:2] floatValue]/255.0;
CGFloat alpha = [[colorComponents objectAtIndex:3] floatValue];
UIColor *color = [[UIColor alloc]initWithRed:red
green:green
blue:blue
alpha:alpha];
//UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return color;
}
bgView.backgoroundColor = [[self class]colorWithValues:@"155,124,144,1"];
func colorWithValues(#colorToString : String) -> (UIColor) {
var colorString = colorToString.componentsSeparatedByString(",")
let redColor : Float = Float(colorString[0].toInt()!)/255.0
let greenColor : Float = Float(colorString[1].toInt()!)/255.0
let blueColor: Float = Float(colorString[2].toInt()!)/255.0
let colorAlpha: Float = Float(colorString[3].toInt()!)
var colorValue : UIColor! = UIColor(red:CGFloat(redColor), green:CGFloat(greenColor), blue:CGFloat(blueColor), alpha: colorAlpha)
return colorValue
}
As I can see you forgot to wrap alpha
in CGFloat
var colorValue : UIColor! = UIColor(red:CGFloat(redColor), green:CGFloat(greenColor),
blue:CGFloat(blueColor), alpha: colorAlpha)
^^^^^^^^^^ here
Which causes the problem, change it to this
var colorValue : UIColor! = UIColor(red: CGFloat(redColor), green: CGFloat(greenColor),
blue: CGFloat(blueColor), alpha: CGFloat(colorAlpha))