CGpoint Vs CGPointMake

user2725255 picture user2725255 · Aug 21, 2014 · Viewed 12.3k times · Source

I have recently been introduced to swift.

A question, when to use CGPoint and when shall I use CGPointMake ?

I have interchanged these two statements and they both returned the same result

 self.anchorPoint  = CGPointMake(0.5, 0.5)

 self.anchorPoint  = CGPoint(0.5, 0.5)

Thanks

Answer

rickster picture rickster · Aug 21, 2014

Every struct in Swift gets an automatically created member-wise initializer. So, because struct CGPoint exists with members x and y, you can do:

 self.anchorPoint  = CGPoint(x: 0.5, y: 0.5)

(Note that you can't actually do CGPoint(0.5, 0.5) -- that gets a compile error because initializers require labels for all parameters, except where otherwise declared.)

In Swift 1.x-2.x, CGPointMake was a function imported from the C version of this API. In Swift 3, the initializer form is the only way to create a CGPoint -- it's one of the changes to make CoreGraphics much more Swifty.