NEW: Master Swift design patterns with my latest book! >>

How to compare two CGRects with equalTo()

Written by Paul Hudson    @twostraws

You could compare two CGRect values by evaluating their X, Y, width and height values, but there's a much faster way: equalTo(). This takes two rects as its only two parameters and returns true if they are the same, or false otherwise.

Here's an example:

let rect1 = CGRect(x: 64, y: 64, width: 128, height: 128)
let rect2 = CGRect(x: 256, y: 256, width: 128, height: 128)

if rect1.equalTo(rect2) {
    // rects equal!
} else {
    // rects not equal
}

Available from iOS 2.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Get the ultimate experience

The Swift Power Pack includes my first six books for one low price, helping you jumpstart a new career in iOS development – check it out!

Click here to visit the Hacking with Swift store >>