简体   繁体   中英

Displaying the contents of a CSV file in a UIPicker

I was wondering what the best way of displaying the contents of a CSV file in a UIPicker would be.

The idea is that a person picks a date range in a UIPicker and then gets a UIAlert popup based on their selection.

I know there are probably a few options. I thought there might be something simple where I can avoid making a database.

My CSV file has 4 columns. One of the columns is a date range column and is written like:

Feb 20, 1920 - Feb 7, 1921
Feb 8, 1921 - Jan 27, 1922
Jan 28, 1922 - Feb 15, 1923
Feb 16, 1923 - Feb 4, 1924
etc....

thanks for any help.

You could build an incredibly simple CSV parser just using methods of NSString .

You can read in the contents on the CSV using

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

You can then split the string to get an array where each object represents a line in the file using

- (NSArray *)componentsSeparatedByString:(NSString *)separator

passing @"\n" as the separator

Once you have this array, you can then loop through it and use the same method to split each line up by passing @"," as the separator ( assuming your columns are comma delimited, although in your case it seems the delimeter may be something else? )

NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *rows = [contents componentsSeparatedByString:@"\n"];
for (NSString *row in rows) {
    NSArray *values = [row componentsSeparatedByString:@","];
    // do something with the values here
}

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