Search code examples
iosswiftcgfloatcgsize

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?


Solution

  • 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.