What is the difference between using CGSizeMake and CGSize? Better than the other?

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

+5
source share
3 answers

The CGSize constructor is a Swift extension to CGSize :

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

CGSizeMake is a residual built-in function related to Objective-C:

 /*** Definitions of inline functions. ***/ // ... public func CGSizeMake(width: CGFloat, _ height: CGFloat) -> CGSize 

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

+11
source

While the functionality they make no difference, CGSizeMake() is / will be deprecated. It's easier to write and read CGSize() , so Apple prefers to use CGSize() for the future.

+2
source

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

But if you are talking about the structural and programmatic differences between these two points, then the structure and explanation of the code is also presented here.

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

In the first case here, i.e. CGSize() , the code clearly demonstrates that its structure, which usually takes height and width like CGFloat() , is a distance vector, but not a physical size. As a vector, the value may be negative.

On the other hand, in the case of CGSizeMake() , we can easily understand what its function is, not its structure. It usually 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 the case of pure quick use and the CGSize() code is much simpler and more understandable than CGSizeMake() . You can get this from the above example correctly .. !!!!

Thanks,

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1241370/


All Articles