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

3

5

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?

Sanguine answered 24/1, 2016 at 21:57 Comment(1)
T
11

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.

Trantham answered 24/1, 2016 at 22:0 Comment(9)
So is one preferred over the other? Thanks!Sanguine
It's all preference, although I've found that working with a pure Swift codebase CGSize(...) is cleaner than CGSizeMake(...).Trantham
Cool thanks @JAL! Unfortunately must wait 9 min to accept your answer :)Sanguine
@Sanguine Sure, I'll take a look at those as I have time this week. Happy hacking!Trantham
I'm curious about how this works out with swift. From developer.apple.com/library/ios/documentation/GraphicsImaging/… "your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.". Talking about functions like CGRectGetMidX CGRectGetWidth and the like.Jaquiss
@AndrewCarter Those inline CGRectGet[X] functions should still work the same way in Swift, although I'm not really sure if that's what you're asking. If that's not what you're asking, you should ask a new question.Trantham
I'm wondering if Apple's recommendation to not directly access the data in the CGRect is still valid. Maybe I will ask a new question :)Jaquiss
@AndrewCarter Ah, I see what you're asking now. Please ask a new question and I will definitely answer it!Trantham
@AndrewCarter (and anyone else who is curious): I've attempt to answer any questions about CGRect normalization in Swift here: https://mcmap.net/q/1922664/-accessing-cgrect-values-directly-vs-normalizing-them-in-swift-objective-c-rules-still-valid/2415822Trantham
M
2

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.

Midway answered 24/1, 2016 at 22:7 Comment(3)
"CGSizeMake() is/will be depreciated" can you back this claim up with a creditable source?Trantham
I haven't seen them included in the latest apple swift and foundation reposMidway
FYI - you mean "deprecated", not "depreciated". Very different meanings. :)Whipperin
D
1

So in case if you are talking about the difference on usability aspect then there is no basic difference between CGSize() and CGSizeMake().

But if you are talking about structural and programatic difference between this twos then here is some code snippet structure and explanation as well.

  1. CGSize()

    struct CGSize { var width: CGFloat var height: CGFloat init() init(width width: CGFloat, height height: CGFloat) }
    
  2. CGSizeMake()

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

Explanation:-

On the first case here i.e. CGSize(), the code clearly demonstrates that its a structure which generally takes height and width as CGFloat()and represent a distance vector but not a physical size. As a vector the value could be negative.

On another hand in case of CGSizeMake(), we can easily understand that its a function rather than a structure. It generally takes height and width as CGFloat()and returns a CGSize() structure.

Example:-

  1. CGSize()

    var sizeValue = CGSize(width: 20, height: 30) //Taking Width and Height
    
  2. CGSizeMake()

    var sizeValue = CGSizeMake(20,30) //Taking Width and Height too but without named parameters
    

Now in case of pure swift usage and code CGSize() is much simpler and understandable than CGSizeMake(). You can get that from the above example right..!!!!

Thanks,

Hope This Helped.

Dihedron answered 5/2, 2016 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.