iOS how does NSMutableArray check if it contains NSNumber objects?
Solution 1
What you are doing is fine. Why wouldn't it be?
The containsObject:
method actually iterates over the array and calls the isEqual:
method on each object passing in the object you are checking for.
BTW - there is nothing special here about using NSNumber
. It's the same with an array of any object type. As long as the object's class has a valid isEqual:
method, it will work.
Solution 2
Per the Apple's NSNumber documentation, you should use isEqualToNumber
:
isEqualToNumber: Returns a Boolean value that indicates whether the receiver and a given number are equal. - (BOOL)isEqualToNumber:(NSNumber *)aNumber
Alex Stone
When people asked me what I wanted to do for work 10 years ago, I did not know too well, so I just said "Something to do with computers". As I look at the last 10 years, I see that I did quite a lot of work all kinds of computers. From fiddling with microcontrollers and assembler code to writing data processing scripts to physically assembling computer consoles. The big step forward came when I learned to think about software in a structured, object-oriented way, as this allowed me to make software do things that I want it to do. Right now I'm proficient in two high level programming languages - Objective-C and Java and have touched just about every framework available for iOS. I've also became a hacker/maker and have completed a number of projects involving software and hardware. Right now I'm in my early 30s and when I ask myself "What would I like to do in the next 10 years?", my answer is "something with the human brain". The seeds are already there - I've picked up an interest in biology, cognitive science and neuroscience, enough to converse with real people. I've done first-hand research into sleep and made discoveries. I've taken classes in synthetic biology, performing manipulations on the bacteria genome. I've learned a lot about the neurotransmitter systems of the human brain, as well as how a biological organism develops. It seems like there are a lot of similarities between the object-oriented concepts I use in the daily programming tasks and how biological organisms operate. This makes me hopeful that by the time I'm in my late 30s, I would be able to work and program some form of biological computer or just plain hack the human brain.
Updated on June 04, 2022Comments
-
Alex Stone almost 2 years
I'm writing some code that will be using NSMutableArray and storing int values within it, wrapped within NSNumbers.
I would like to confirm that querying an iOS NSArray or NSMutableArray using new NSNumbers with same values is legal, of if I need to explicitly iterate over the array, and check if each int value is equal to the value I want to test against?
This appears to work:
NSMutableArray* walkableTiles = [NSMutableArray array]; [walkableTiles addObject:@(1)]; [walkableTiles addObject:@(2)]; [walkableTiles addObject:@(3)]; if([walkableTiles containsObject:@(1)]) { DLog(@"contains 1"); //test passes } if([walkableTiles containsObject:[NSNumber numberWithFloat:2.0]]) { DLog(@"contains 2");//test passes } if([walkableTiles containsObject:[NSNumber numberWithInt:3]]) { DLog(@"contains 3");//test passes }
-
Hot Licks over 10 yearsBut isEqual will work just fine. It simply isn't as tightly typed.
-
Hot Licks over 10 years(NSNumber conforms to NSObject, where isEqual is defined.)