0

I have a long NSString, something like "t00010000t00020000t00030000" and so on. I need to split that up into each "t0001000".

I'm using...

NSArray *tileData = [[GameP objectForKey:@"map"] componentsSeparatedByString:@"t"];

And it splits it up, but the "t" is missing, which I need (although I think I could append it back on). The other way I guess would be to split it up by counting 8 char's, although not sure how to do that.

But ideally I need it split into a [][] type array so I can get to each bit with something like...

NSString tile = [[tileData objectAtIndex:i] objectAtIndex:j]];

I'm new to obj-c so thanks for any help.

3
  • I think you listed the two likely candidates. Commented Oct 28, 2012 at 19:51
  • There's no straightforward way to split the string into an array of arrays? Commented Oct 28, 2012 at 19:52
  • @Phil both of them are quite straightforward. If you can't do either of them out of the box (again, these are two fairly simple algorithms), you seriously need to pick up some algorithmic thinking. Commented Oct 28, 2012 at 19:53

2 Answers 2

1

If they're not strictly the t characters that separate the sections, i. e. the parts are always 8 characters long, then it's very easy to do it:

NSString *string = @"t00010000t00020000t00030000";
NSMutableArray *arr = [NSMutableArray array];
int i;
for (i = 0; i < string.length; i += 8) {
    [arr addObject:[string substringWithRange:NSMakeRange(i, 8)]];
}

and here arr will contain the 8-character substrings.

Edit: so let me also provide yet another solution for the multidimensional one. Of course @KevinH's solution with the characters is very elegant, but if you need an NSString and you don't mind implementing another method, it's fairly easy to add something like this:

@implementation NSString (EightCarAdditions)

- (NSString *)charAsStringInSection:(NSInteger)section index:(NSInteger)index
{
    return [self substringWithRange:NSMakeRange(section * 8 + index, 1)];
}

- (unichar)charInSection:(NSInteger)section index:(NSInteger)index
{
    return [self characterAtIndex:section * 8 + index];
}

@end

(Beware that characterAtIndex returns a unichar and not a char - be prepared for more than 1 byte-wide UTF-(8, 16, 32) stuff.) You can then call these methods on an NSString itself, so it's very convenient:

unichar ch = [string charInSection:1 index:3];
Sign up to request clarification or add additional context in comments.

8 Comments

I'm using... for (int i = 0; i < [[GameP objectForKey:@"groundMap"] length]; i += 8) { [sharedInstance.groundMap addObject:[[GameP objectForKey:@"groundMap"] substringWithRange:NSMakeRange(i, 8)]]; } But it's not working, when I check sharedInstance.groundMap after that line, it contains zero objects.
@Phil Do you initialize it? I suspect no.
You're probably right but for the life of me I can't get sharedInstance.groundMap to initialise. sharedInstance is my singleton but when I "groundMap = [[NSMutableArray alloc] init];" in my GameP init method, I get the error "instance variable 'groundMap' accessed in class method"
@Phil In this case, please read the relevant part of an Objective-C tutorial. Or alternatively think about it a bit. It makes no sense to access an instance variable from a class method. Write groundMap = [[NSMutableArray alloc] init]; in the init method.
Yes but in the init method of the singleton, in the code there's a comment that says "initialise variables here" so that's where I thought you do that, but it's not working.
|
1

H2CO3's answer is spot-on for the first part. For the second (the multi-dimensional array), if I understand what you want, you don't need another array for the individual characters. For each NSString in the array, you can access each character by calling characterAtIndex. So, extending the example above:

for (NSString *item in arr) {
    NSLog(@"The fifth character of this string is: %C", [item characterAtIndex:4]);
}

And if you're looking to chain these together, as in your example, you can do that too:

NSLog(@"The fifth character of the fourth string is: %C",
    [[arr objectAtIndex:3] characterAtIndex:4]);

3 Comments

By the way, logging the unichar return value of characterAtIndex: is undefined behavior.
String format specifications in Objective-C ultimately follow the IEEE printf specification, which allows for the %c conversion of an int (superset of unsigned short, which is the typedef for unichar) to an unsigned char.
Though, to be fair to your point, Apple's docs recommend using %C for unichar characters, so that's probably more appropriate. I'll update the answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.