-4

I like to tokenize a string to characters and store the tokens in a string array. I am trying to use following code which is not working as I am using C notation to access the array. What needs to be changed in place of travel path[i]?

NSArray *tokanizedTravelPath= [[NSArray alloc]init];
for (int i=0; [travelPath length]; i++) {
    tokanizedTravelPath[i]= [travelPath characterAtIndex:i];
ThomasW
  • 16,981
  • 4
  • 79
  • 106
pbd
  • 423
  • 3
  • 8
  • 19

3 Answers3

1

You need a NSMutableArray to set every element of the array (otherwise you can't change its objects).Also, you can only insert objects in the array, so you can:
- Insert a NSString containing the character;
- Use a C-style array instead.
This is how to do with the NSMutableArray:

NSMutableArray *tokanizedTravelPath= [[NSMutableArray alloc]init];
for (int i=0; i<[travelPath length]; i++) 
{
    [tokanizedTravelPath insertObject: [NSString stringWithFormat: @"%c", [travelPath characterAtIndex:i]] atIndex: i];
}
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
1

You can't store unichars in an NSArray*. What exactly are you trying to accomplish? An NSString* is already a great representation for a collection of unichars, and you already have one of those.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
1

I count 3 errors in your code, I explain them at the end of my answer.
First I want to show you a better approach to split a sting into it characters.


While I agree with Kevin that an NSString is a great representation of unicode characters already, you can use this block-based code to split it into substrings and save it to an array.

Form the docs:

enumerateSubstringsInRange:options:usingBlock:
Enumerates the substrings of the specified type in the specified range of the string.

NSString *hwlloWord = @"Hello World";
NSMutableArray *charArray = [NSMutableArray array];
[hwlloWord enumerateSubstringsInRange:NSMakeRange(0, [hwlloWord length])
                              options:NSStringEnumerationByComposedCharacterSequences
                           usingBlock:^(NSString *substring,
                                        NSRange substringRange,
                                        NSRange enclosingRange,
                                        BOOL *stop)
{
    [charArray addObject:substring];
}];
NSLog(@"%@", charArray);

Output:

(
    H,
    e,
    l,
    l,
    o,
    " ",
    W,
    o,
    r,
    l,
    d
)

But actually your problems are of another nature:

  • An NSArray is immutable. Once instantiated, it cannot be altered. For mutable array, you use the NSArray subclass NSMutableArray.

  • Also, characterAtIndex does not return an object, but a primitive type — but those can't be saved to an NSArray. You have to wrap it into an NSString or some other representation.

    You could use substringWithRange instead.

    NSMutableArray *tokanizedTravelPath= [NSMutableArray array];
    for (int i=0; i < [hwlloWord length]; ++i) {
        NSLog(@"%@",[hwlloWord substringWithRange:NSMakeRange(i, 1)]);
        [tokanizedTravelPath addObject:[hwlloWord substringWithRange:NSMakeRange(i, 1)]];
    }
    
  • Also your for-loop is wrong, the for-loop condition is not correct. it must be for (int i=0; i < [travelPath length]; i++)

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178