Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGpoint Vs CGPointMake

Tags:

swift

cgpoint

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

like image 630
user2725255 Avatar asked Aug 21 '14 17:08

user2725255


People also ask

What is a CGPoint?

A structure that contains a point in a two-dimensional coordinate system.

What is a CGPoint in Swift?

CGPoint is a struct used to represent an x and y coordinate in a 2D plane. This post presents examples of how Lottie and Charts, two popular Cocoapods with over 44,000 combined GitHub stars, use CGPoint in Swift: Compute CGRect Corners and Center. Describe A CoreGraphics Path. Respond To Rotation Gestures.


2 Answers

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.

like image 127
rickster Avatar answered Sep 19 '22 18:09

rickster


When you cmd-click on the type Xcode opens the file with the definition.

CGPoint is a struct:

struct CGPoint {
    var x: CGFloat
    var y: CGFloat
}

and CGPointMake is just a function which creates a CGPoint and returns it:

func CGPointMake(x: CGFloat, y: CGFloat) -> CGPoint
like image 20
zisoft Avatar answered Sep 18 '22 18:09

zisoft