Swift 3 - How to Write Functions with No Initialisers Like the New Uicolors

Swift 3 - How to write functions with no initialisers like the new UIColors?

You can use computed properties:

extension UIColor {
static var custom: UIColor {
return UIColor(white: 0.5, alpha: 1)
}
}

How to add color to a view in swift3?

If you working in Swift 3 it should be as below:

let firstFrame = CGRect(x: 160, y: 240, width: 100, height: 150)
let firstView = UIView(frame: firstFrame)
firstView.backgroundColor = UIColor.blue
view.addSubview(firstView)

let secondFrame = CGRect(x: 20, y: 30, width: 50, height: 50)
let secondView = UIView(frame: secondFrame)
secondView.backgroundColor = UIColor.green
view.addSubview(secondView)

Swift omit class on initalization

You can simply write an extension for UIColor in order to do this. For example:

extension UIColor {

// your custom color function
class func customColor() -> UIColor {
return UIColor(red: 0.3, green: 0.4, blue: 0.5, alpha: 0.6)
}
}

...

self.backgroundColor = .customColor()

Although I had no idea that convenience syntax was in the language... thought it was only for enums. So thanks for showing me that!

Two init functions with UIColor ios

The convenience initializer is used for color literals (as the argument label says). You can see it in both Xcode and in Playgrounds when you drag and drop a color.

I don't see any reason to call the color literal initializer yourself. Instead you'd most likely use either Option 1 or perhaps UIColor(displayP3Red:green:blue:alpha:) if you wanted to use the Display P3 color space.

Swift UIColor initializer - compiler error only when targeting iPhone5s

It probably has to do with a mismatch between Float and Double. Basically, if the arguments are expected to be CGFloats, you need to pass CGFloats.

let color = UIColor(red: CGFloat(red), green: CGFloat(green), blue: CGFloat(blue), alpha: CGFloat(alpha))

What's important to understand here is that CGFloat is defined as Float on 32 bit platforms, and Double on 64 bit platforms. So, if you're explicitly using Floats, Swift won't have a problem with this on 32 Bit platforms, but will produce an error on 64 bit.

How to create several cached UIColor

You can use the same approach as in
Using a dispatch_once singleton model in Swift, i.e. static
constant stored properties
which are initialized lazily (and only once). These can be defined
directly in the UIColor extension:

extension UIColor {
convenience init(hex: String) {
// ...
}

static let myColorOne = UIColor(hex:"AABBCC")
static let myColorTwo = UIColor(hex:"DDEEFF")
}

How to create a hex color string UIColor initializer in Swift?

Xcode 9 • Swift 4 or later

extension UIColor {
convenience init?(hexaRGB: String, alpha: CGFloat = 1) {
var chars = Array(hexaRGB.hasPrefix("#") ? hexaRGB.dropFirst() : hexaRGB[...])
switch chars.count {
case 3: chars = chars.flatMap { [$0, $0] }
case 6: break
default: return nil
}
self.init(red: .init(strtoul(String(chars[0...1]), nil, 16)) / 255,
green: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
blue: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
alpha: alpha)
}

convenience init?(hexaRGBA: String) {
var chars = Array(hexaRGBA.hasPrefix("#") ? hexaRGBA.dropFirst() : hexaRGBA[...])
switch chars.count {
case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
case 6: chars.append(contentsOf: ["F","F"])
case 8: break
default: return nil
}
self.init(red: .init(strtoul(String(chars[0...1]), nil, 16)) / 255,
green: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
blue: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
alpha: .init(strtoul(String(chars[6...7]), nil, 16)) / 255)
}

convenience init?(hexaARGB: String) {
var chars = Array(hexaARGB.hasPrefix("#") ? hexaARGB.dropFirst() : hexaARGB[...])
switch chars.count {
case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
case 6: chars.append(contentsOf: ["F","F"])
case 8: break
default: return nil
}
self.init(red: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
green: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
blue: .init(strtoul(String(chars[6...7]), nil, 16)) / 255,
alpha: .init(strtoul(String(chars[0...1]), nil, 16)) / 255)
}
}

if let textColor = UIColor(hexa: "00F") {
print(textColor) // r 0.0 g 0.0 b 1.0 a 1.0
}

if let textColor = UIColor(hexaRGB: "00F") {
print(textColor) // r 0.0 g 0.0 b 1.0 a 1.0
}

UIColor(hexaRGB: "#00F") // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGB: "#00F", alpha: 0.5) // r 0.0 g 0.0 b 1.0 a 0.5

UIColor(hexaRGB: "#0000FF") // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGB: "#0000FF", alpha: 0.5) // r 0.0 g 0.0 b 1.0 a 0.5

UIColor(hexaRGBA: "#0000FFFF") // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaRGBA: "#0000FF7F") // r 0.0 g 0.0 b 1.0 a 0.498

UIColor(hexaARGB: "#FF0000FF") // r 0.0 g 0.0 b 1.0 a 1.0
UIColor(hexaARGB: "#7F0000FF") // r 0.0 g 0.0 b 1.0 a 0.498

When writing a large app in Swift, do I even need to use the init method at all?

Lets assume you have a factory that builds cars and a tool / class "BuildCar" with the properties:

var numberOfTires =  4

var color = UIColor.red()

var maxSpeed = 100// mph

Now you want to build a whole lot of your fancy cars and use your 'BuildCar' Tool to do so. All you can do is build red cars that have 4 tires, and have a max speed of 100 mph.

But what if you want to build a new car? A blue one, with 4 tires and max speed of 120? If you used init, you could change the variables easily.

How to call Swift initializer methods with multiple param in Objective-C

To be accessible and usable in Objective-C, a Swift class must be a
descendant of an Objective-C class or it must be marked @objc.

And also this answer.

This apple doc will also help you understand.

So the options for you is to either inherit the class of yours from some Objective C class (In this case compiler adds @objc) or add @objc yourself to your class.

You also need to add to your Objective C class-

#import "<#ProjectName#>-Swift.h"

Real solution to this specific question

Please look at this question, you will get your solution yourself. :)

That question says that optional parameters can't be exposed from Swift to Objective C as all parmeters become _Nonnull.

So I think you will need to create another initializer without optionals and default parameters.



Related Topics



Leave a reply



Submit