Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: Check if two NSArrays are equal

Tags:

cocoa

nsarray

I have two NSArrays of NSRects (stored using NSStringFromRect(NSRect)). Is there a quick way to check and see if the items in the array are equal or will I have to do a loop? So item 1 in array 1 = item 1 in array 2, etc. etc.

Thanks

like image 395
PruitIgoe Avatar asked Dec 06 '11 18:12

PruitIgoe


People also ask

How do you check if two arrays are equality?

The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.

How do I check if two arrays are equal in Python?

To check if two NumPy arrays A and B are equal: Use a comparison operator (==) to form a comparison array. Check if all the elements in the comparison array are True.

How do I check if two arrays are equal in Swift?

To check if two arrays are equal in Swift, use Equal To == operator. Equal To operator returns a Boolean value indicating whether two arrays contain the same elements in the same order. If two arrays are equal, then the operator returns true , or else it returns false .

How do you compare two arrays equal in Java?

Arrays. equals(Object[] a, Object[] a2) method returns true if the two specified arrays of objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal.


2 Answers

If you check the NSArray Reference, you'll find a handy -isEqualToArray: method that should do just what you want

like image 99
David Gelhar Avatar answered Sep 23 '22 13:09

David Gelhar


From the documentation for -[NSArray isEqualToArray:]:

Compares the receiving array to another array. Two arrays have equal contents if they each hold the same number of objects and objects at a given index in each array satisfy the isEqual: test.

This is exactly what you are looking for.

like image 42
Ole Begemann Avatar answered Sep 24 '22 13:09

Ole Begemann