With Swift 3.0, we now declare CGPoints like so:
CGPoint(x: 1.0, y: 0.0)
This is, uh...kinda verbose. When you write a function that takes a CGPoint as an argument, things get ugly pretty fast:
myObject.callMyFunc(someVar: 1, point1: CGPoint(x: 1.0, y: 0.0), point2: CGPoint(x: 1.0, y: 2.0))
Is there any shorthand for structs like CGPoint and CGRect?
You could extend CGPoint to
ExpressibleByArrayLiteral
protocol, then you could use an array literal to initialize an CGPoint:the code:
You should handle the element count of the array well, because complier won't check it for you. Even though this method breaks the compile safety, it's a very convenient method to use.
There is already a lib (Literal) implement this for you. It have handled some fall-down situations, like pass a one-element array to a variable where CGPoint required.
As already suggested in the comments you can create your own extension.
Extension
You can add an initializer that accepts 2
CGFloat(s)
and doesn't require external param namesInfix Operator
You can also define your infix operator