Some fun with NSNumbers

 

I have an application that I work on just for kicks, adding new “features” and discovering how things work with Cocoa. Today I tackled one of my larger fears to date; NSNumber.

I’m sure there are some seasoned Cocoa programmers out there wondering about my confusion over this class, but it was kinda scary. I’m very comfortable writing plain C and feel very powerful because I know what is going on and if I get a little stumped, I can always hack something homemade together. That’s not really the way it is a lot of times with Cocoa, there is so much already available you don’t have to hack it together and it would be better if you didn’t. So back to NSNumbers.

Just to get your brains working this morning, here was a preview if() statement I had in my code, just checking to see if the number that the user entered in an NSTextField was of appropriate range.

1
2
3
if ( [startingValue intValue] >= 300 || startingValueInt <= 0) {
	// Do something
}

This doesn’t seem too bad at all to me. I need the value to be between 1 and 300. However, I decided to get a bit more specific with my code and created an NSArray of NSNumbers holding the upper limits to check against in each specific case. This was used to add that in the Category object itself and then you come up with the following if statement, if you will:

1
2
3
4
// This was just a very lengthy way of using NSNumbers to make sure that the entered value was in proper range
if ( [[NSNumber numberWithInt:startingValueInt] compare: [[categories objectAtIndex: [CategoriesController selectionIndex]] categoryMaxValue]] == NSOrderedDescending || startingValueInt <= 0) {
	// Do something
}

Now granted, you could still use a regular “>=” operator here by calling the “- intValue” method on the NSNumbers I’m using. However, I thought that I would go all out and even use an NSComparisonResult as well. Just goes to show you that you can look smart and your code do nothing at all. Another snippet of code is below for those that wondered about getting all of my NSNumbers into my Category array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Setup another array with just the ranges for each category — This is the max array, all mins are zero
// Note that I have setup similar arrays holding the names and urls that I’m sticking into each Category ā€˜c’
 
NSArray* maxes = [NSArray arrayWithObjects:[NSNumber numberWithInt:445], [NSNumber numberWithInt: 265],
	[NSNumber numberWithInt:221], [NSNumber numberWithInt:106], [NSNumber numberWithInt:69],
	[NSNumber numberWithInt:21], [NSNumber numberWithInt:229], [NSNumber numberWithInt:69],
	[NSNumber numberWithInt:322], [NSNumber numberWithInt:266], [NSNumber numberWithInt:138],
	[NSNumber numberWithInt:320], [NSNumber numberWithInt:107], [NSNumber numberWithInt:99],
	[NSNumber numberWithInt:313], nil];
 
// Now need to create the categories, using each piece to make one, and insert it into the categories array
for (NSInteger i = 0; i < [names count]; i++) {
	c = [Category new];
	[c setCategoryUrl: [urls objectAtIndex:i]];
	[c setCategoryName:[names objectAtIndex:i]];
	[c setCategoryMaxValue:[maxes objectAtIndex:i]];
	[categories insertObject: c atIndex: i];
}

More Reading | Cocoa Dev Central

Comments (0) Leave a Comment
  1. No comments yet.

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">
  1. No trackbacks yet.