6

I have an NSarray called array. And it look like this

array = @[@"one", @"two", @"three"];

I want this array to be capitalized. What is the best way to go about this. I can only think of making an NSMutableArray called mutableArray.

And do something like this

for(int i = 0; i < array.lenght; i++) {
    self.mutableArray = addObject:[array[i] capitalizedString];
}

Or is there another better way?

iqueqiorio
  • 1,149
  • 2
  • 35
  • 78

5 Answers5

16

The magic method you are looking for does in fact exist.

NSArray *array = @[@"one", @"two", @"three"];
NSArray *capArray = [array valueForKeyPath:@"capitalizedString"];
Krys Jurgowski
  • 2,871
  • 17
  • 25
  • 3
    If anyone wants to UPPERCASE then use `"uppercaseString"` instead of `"capitalizedString"`. – Ganpat Jul 09 '18 at 08:58
1

SWIFT

You Can use map

let array = ["one", "two", "three"]
let upercaseArray = array.map({$0.uppercased()})

now you have upercaseArray like ["ONE","TWO","THREE""]

0

What you really want is a sort of transform method, which takes an array and a selector, then returns an array of the results of performing that selector on each object. Unfortunately that doesn't exist in vanilla objective-C.

Your approach is generally fine, but I would be careful of two points. Firstly, make sure you create the NSMutableArray with the capacity of the NSArray you are copying, as this will avoid any reallocation overhead as you add objects to it. Secondly, you might want to copy the mutable array so you end up with an immutable NSArray as the final result.

So I would use something like this:

- (NSArray *)capitalizeStringArray:(NSArray *)array {
    // Initialize tempArray with size of array
    NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:array.count];
    for (NSString *str in array) {
        [tempArray addObject:[str capitalizedString]];
    }
    return [tempArray copy]; // convert back to NSArray]
}

You can convert this to a category method on NSArray if you like, and generalize it to use other selectors if you wish.

Jsdodgers
  • 5,253
  • 2
  • 20
  • 36
Alex MDC
  • 2,416
  • 1
  • 19
  • 17
0

There's about a gazillion ways to handle this. For small arrays, pick whichever you find easier to understand.

I'd probably use code like this:

- (NSMutableArray *) capitalizedArrayFromArrayOfStrings: (NSArray*) array;
{
  NSMutableArray *result = [NSMutableArray arrayWithCapacity: array.count];
  for (NSString *string in array)
  {
    if ([string isKindOfClass: [NSString class]]
      [result addObject: [string capitalizedString];
  }
}

Creating your array with the correct capacity at the beginning enables the array to allocate enough space for all it's future elements and saves it having to allocate more space later.

Using for..in fast enumeration syntax is more efficient than using array indexing, but for short arrays the difference is small. The code is also simpler to write and simpler to read, so I prefer that syntax where possible.

As Alex says, you could also create a category method on NSArray that would return a capitalized version of your array, or even a category on NSMutableArray that would replace the strings in the array "in place".

Duncan C
  • 128,072
  • 22
  • 173
  • 272
-1

Works like charm.

NSString *myString = YOUR_ARRAY.uppercaseString;
[myNSMutableArray addObject:myString];
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
  • The OP clearly states they are looking for a solution to manipulate an already existing array of multiple strings to an array of each string capitalized... sry – Will Von Ullrich Jan 19 '18 at 14:18