tags:

views:

169

answers:

2

Is the there a way to multiply each NSNumber contained in the array by 10? Here is what I have so far:

NSMutableArray *vertex = [NSMutableArray arrayWithCapacity:3];

[vertex addObject:[NSNumber numberWithFloat:1.0]]; 
[vertex addObject:[NSNumber numberWithFloat:2.0]];
[vertex addObject:[NSNumber numberWithFloat:3.0]];

[vertex makeObjectsPerformSelector:@selector(doSomethingToObject:)];

I am not sure what selector to use to do this, please help!

A: 

Not easily, no. You would have to loop through the entire thing and replace all those instances of NSNumber with new instances of NSNumber (NSNumber itself is immutable). So, for example:

for( int i = 0; i < [vertex count]; i++ )
  [vertex replaceObjectAtIndex:i withObject:[NSNumber numberWithFloat:[[vertex objectAtIndex:i] floatValue] * 10.0f]];

Obviously this is rather hard to read. You are probably better off just using a regular, primitive array of floats if you are going to be manipulating them often (e.g., applying transformations to them).

Jason Coco
A: 

Short answer - no.

NSNumber of merely a container for a primitive value. It does not do any mathematical work. The makeObjectsPerformSelector method can be used to tell each object in the array to do something. But the class of each of those objects has to have the method the selector is for. NSNumber also does not provide any method for changing the stored value. So even if you added a category to NSNumber to do the math, you would still have to replace the old value in the array with the newly computed one.

I think a better solution would be to add a category method to NSMutableArray to do the work. It would look through the contents, calculate each new value and then replace each array member with the new one.

Derek Clarkson
Thanks for you replies, guys! @Jason: Using NSMutableArray allows me to append values at the end. I am not sure how to implement same using a primitive array. The solution I figured out is similar to yours. NSMutableArray *vertex2 = [[NSMutableArray alloc] init]; int i=0; for(NSNumber * flt in vertex) {[vertex2 addObject:[NSNumber numberWithFloat:[[vertex objectAtIndex:i] floatValue]*10]]; i++; }I need to keep the original array. In short, the first array is the vertices of the unit sphere, the second is their transformation.@Derek: What's a category method? Is it easier to read?
seaworthy
Lookup categories in your friendly Objective C manual. Basically a category is a way to add/append a class with additional methods without extending it's hierarchy. For example, it's a way to add additional methods to NSArray. Then when you need those methods you do an import of your categories header file (just like importing a class .h file) and the methods you have defined in it are instantly available on every NSAarry class instance. It's a great way to augment base classes without creating extensions. The only limit is you cannot add variables, only methods.
Derek Clarkson
You can find apples documentation on them at http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html
Derek Clarkson