简体   繁体   中英

Objective - C / Cocoa Application text file parse

I am trying to create a cocoa application for Mac OS X in which the user chooses a text file and the columns of the text file are put into a table on the GUI. The text files will always have similar formatting.

Basically, I would really appreciate an overview of (1) how I could parse the data from the text file and then (2) how I can arrange the data for display and/or future manipulation.

For example the text file could be of the form:

c18                 10706      463029             A
c2                  10841      91075              G
c16                 11164      .                  A
c19                 11257      41553              C 

I appreciate any help with this! I am familiar how to do this in MATLAB or even bash script, but I'm just having trouble getting pass the "beginners" stage of X-Code/Objective-C/Cocoa. Thanks!

For the parsing, something like:

NSScanner *scanner = [[NSScanner alloc] initWithString:...];
NSMutableArray *rows = [NSMutableArray array];
NSArray *columnNames = @[@"one", @"two", @"three", @"four"];
NSCharacterSet *whiteSpaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSCharacterSet *allowedSet = [whiteSpaceSet invertedSet];

while (![scanner isAtEnd) {

    NSMutableDictionary *row = [NSMutableDictionary dictionary];

    for (NSString *columnName in columnNames) {

         NSString *content = nil;

        [scanner scanUpToCharactersFromSet:allowedSet intoString:&content];

        [row setObject:content forKey:columnName];

        [scanner scanUpToCharactersFromSet:whiteSpaceSet intoString:nil];
    }

    [rows addObject:row];
}

Obviously you can change the column names around.

(typed freehand so check names and format).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM