IggY
IggY

Reputation: 3125

Objective C: splitting an array into subarrays

I have a text file looking like this :

#AAA:x
12
34
7
...
#BBB:y
-74.7
-33.2
14
...
#CCC:z
32.4
17
...
#END

I'm able to put all of it in one big NSArray (using componentsSeparatedByString:@"\n")

Now I'd like to have:

AAA float NSArray with all the values under the tag #AAA:x;

BBB float NSArray with all the values under the tag #BBB:y; etc..

How can I do this?

Upvotes: 1

Views: 2106

Answers (1)

DrummerB
DrummerB

Reputation: 40211

To elaborate on my comment, try this:

NSMutableArray *subarrays = [[myTest componentsSeparatedByString:@"#"] mutableCopy];
for (int i = 0; i < subarray.length; i++) {
    NSArray *subarray = [subarrays[i] componentsSeparatedByString:@"\n"];
    subarray = [subarray subarrayWithRange:NSMakeRange(1, subarray.length-1)];
    subarrays[i] = subarray;
}

This should result in an array of string arrays.

So subarrays[0] will be an array of strings with these elements: 12, 34, 7. subarrays[1][2] will be a string "14"

If you want floats and not strings, you will have to additionally iterate over all the entries and convert them to float. You can use NSString's floatValue method to do that.

Upvotes: 3

Related Questions