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?
-
Similar: [Why does swift provide both a CGRect initializer and a CGRectMake function?](http://stackoverflow.com/questions/34717650/why-does-swift-provide-both-a-cgrect-initializer-and-a-cgrectmake-function). – Martin R Jan 24 '16 at 22:05
3 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.

- 41,701
- 23
- 172
- 300
-
-
1It's all preference, although I've found that working with a pure Swift codebase `CGSize(...)` is cleaner than `CGSizeMake(...)`. – JAL Jan 24 '16 at 22:02
-
Cool thanks @JAL! Unfortunately must wait 9 min to accept your answer :) – Crashalot Jan 24 '16 at 22:03
-
@Crashalot Sure, I'll take a look at those as I have time this week. Happy hacking! – JAL Jan 24 '16 at 22:16
-
I'm curious about how this works out with swift. From https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/#//apple_ref/c/func/CGRectMake "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. – Jan 25 '16 at 17:16
-
@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. – JAL Jan 25 '16 at 17:21
-
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 :) – Jan 25 '16 at 17:27
-
@AndrewCarter Ah, I see what you're asking now. Please ask a new question and I will definitely answer it! – JAL Jan 25 '16 at 17:31
-
@AndrewCarter (and anyone else who is curious): I've attempt to answer any questions about `CGRect` normalization in Swift here: http://stackoverflow.com/q/35608638/2415822 – JAL Feb 24 '16 at 17:15
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.

- 38,543
- 21
- 161
- 168
-
9"CGSizeMake() is/will be depreciated" can you back this claim up with a creditable source? – JAL Jan 24 '16 at 22:07
-
I haven't seen them included in the latest apple swift and foundation repos – Esqarrouth Jan 24 '16 at 22:20
-
3FYI - you mean "deprecated", not "depreciated". Very different meanings. :) – rmaddy Jan 24 '16 at 23:02
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.
CGSize()
struct CGSize { var width: CGFloat var height: CGFloat init() init(width width: CGFloat, height height: CGFloat) }
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:-
CGSize()
var sizeValue = CGSize(width: 20, height: 30) //Taking Width and Height
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.

- 6,500
- 4
- 28
- 37