-5

I have an NSArray with 10 NSStrings inside:

myArray (
         @"21.32",
         @"658.47", 
         @"87.32"...
        )

What's the best way to convert all the strings to an NSDecimalNumber?

Legend
  • 3
  • 1
  • 3
    Did you look at the docs for `NSDecimalNumber`? Do you know how to iterate an array? – rmaddy May 01 '15 at 22:12
  • So I have to do it with a for loop? – Legend May 01 '15 at 22:12
  • There is no other way? – Legend May 01 '15 at 22:13
  • Yes, you need a loop. How else could it be done? – rmaddy May 01 '15 at 22:14
  • That's what I was wondering. Ok thanks!! – Legend May 01 '15 at 22:14
  • @rmaddy For those who is new to ObjC but has an experience in languages like Python it could be quite surprising that looping through the array is the only way to do it. – Avt May 01 '15 at 22:37
  • 1
    @Avt then they should say something about how they'd do it in another language. This question has almost no info & makes it look like he didn't even give the problem a shot. – AdamPro13 May 01 '15 at 23:12
  • 1
    Well, it says to keep you question short and to the point. – Legend May 01 '15 at 23:15
  • In swift you could use a map statement to do this. The closest thing I can think of to a map statement in Objective-C would using valueForKey on the array, but I don't there is a way in Objective-C to apply an arbitrary block/selector to an array's elements and transform it to a different type of array. – Duncan C May 02 '15 at 01:15
  • Of course you could write a function in Swift and call it from your Objective-C if you're against writing a simple for loop... – Duncan C May 02 '15 at 01:15

2 Answers2

1

It would be quite simple with a for loop:

myArray (
         @"21.32",
         @"658.47", 
         @"87.32"...
        )

NSMutableArray *numberArray = [NSMutableArray arrayWithCapacity: myArray.count];
for (aString in myArray)
{
  NSDecimalNumber *aNumber = [NSDecimalNumber decimalNumberWithString: aString];
  [numberArray addObject: aNumber];
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
1

A very simple Category on NSArray will allow you to use map as seen in other languages

@interface NSArray (Functional)

-(NSArray *)map:(id (^) (id element))mapBlock;

@end

@implementation NSArray (Functional)

-(NSArray *)map:(id (^)(id))mapBlock
{
    NSMutableArray *array = [@[] mutableCopy];
    for (id element in self) {
        [array addObject:mapBlock(element)];
    }
    return [array copy];
}


@end

Now you can use -map: in your case like

NSArray *array = @[@"21.32",
                   @"658.47",
                   @"87.32"];

array = [array map:^id(NSString *element) {
    return [NSDecimalNumber decimalNumberWithString:element];
}];

A NSMutableArray in-place variant could be


@interface NSMutableArray (Functional)
-(void)map:(id (^) (id element))mapBlock;
@end

@implementation NSMutableArray (Functional)

-(void)map:(id (^)(id))mapBlock
{
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        self[idx] = mapBlock(obj);
    }];
}

@end

NSMutableArray *array = [@[@"21.32",
                           @"658.47",
                           @"87.32"] mutableCopy];

[array map:^id(NSString *element) {
   return [NSDecimalNumber decimalNumberWithString:element];
}];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178