Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using CGSizeMake and CGSize? Is one better than the other?

CGSize(width: 360, height: 480) and CGSizeMake(360, 480) seem to have the same effect. Is one preferred to the other? What is the difference?

like image 838
Crashalot Avatar asked Jan 24 '16 21:01

Crashalot


People also ask

What is CGSize?

Overview. A CGSize structure is sometimes used to represent a distance vector, rather than a physical size. As a vector, its values can be negative. To normalize a CGRect structure so that its size is represented by positive values, call the CGRectStandardize(_:) function.

What is a CGRect?

A structure that contains the location and dimensions of a rectangle.


2 Answers

The CGSize constructor is a Swift extension on CGSize:

extension CGSize {
    public static var zero: CGSize { get }
    public init(width: Int, height: Int)
    public init(width: Double, height: Double)
}

CGSizeMake is a leftover inline function bridged from Objective-C:

/*** Definitions of inline functions. ***/

// ...

public func CGSizeMake(width: CGFloat, _ height: CGFloat) -> CGSize

Both have the same functionality in Swift, the CGSize constructor is just more "Swifty" than the other and provided as a convenience.

like image 108
JAL Avatar answered Nov 03 '22 01:11

JAL


While as functionality they don't have any difference, CGSizeMake() is/will be deprecated. It is easier to write and read CGSize(), so Apple prefers you to use CGSize() for the future.

like image 32
Esqarrouth Avatar answered Nov 03 '22 01:11

Esqarrouth