/* Parse a TSV string into an array of dictionaries Original Source: (See copyright notice at ) */ /*" Build an array of dictionaries from a TSV (tab-separated values) string. Blank lines and lines starting with "#" are ignored. The first non-empty, non-comment line is assumed to be the keys. Empty lines are not added to the array, so blank lines (or lines with just tabs) are safely ignored. Missing items aren't added to the dictionary. Requires -[NSString componentsSeparatedByLineSeparators]. "*/ - (NSArray *) arrayFromTSV { NSMutableArray *result = [NSMutableArray array]; NSArray *lines = [self componentsSeparatedByLineSeparators]; NSEnumerator*theEnum = [lines objectEnumerator]; NSArray *keys = nil; int keyCount = 0; NSString *theLine; while (nil != (theLine = [theEnum nextObject]) ) { if (![theLine isEqualToString:@""] && ![theLine hasPrefix:@"#"]) // ignore empty lines and lines that start with # { if (nil == keys) // Is keys not set yet? If so, process first real line as list of keys { keys = [theLine componentsSeparatedByString:@"\t"]; keyCount = [keys count]; } else // A data line { NSMutableDictionary *lineDict = [NSMutableDictionary dictionary]; NSArray *values = [theLine componentsSeparatedByString:@"\t"]; int valueCount = [values count]; int i; for ( i = 0 ; i < keyCount && i < valueCount ; i++ ) { NSString *value = [values objectAtIndex:i]; if (nil != value && ![value isEqualToString:@""]) { [lineDict setObject:value forKey:[keys objectAtIndex:i]]; } } if ([lineDict count]) // only add the line if there was any data { [result addObject:lineDict]; } } } } return result; }