iphone - NSScanner not importing data -


I am trying to load a default group of data from a CSV file into my core data db. So initially I am trying to read in a CSV file and trying to output it to the log before adding it to the original data database.

This is what I am using;

// Find the import file; NSString * defaultCSVPath = [[NSBundle main bundle] pathForResource: @ "data" type: @ "CSV"]; // Get data in a string NSString * fileString = [NSString stringWithContentsOfFile: Default CSVPath encoding: NSUTF8StringEncoding Error: Zero]; If (Zero == file string) {NSLog (@ "Could not open file data CDS"); Abortion (); } // Create a Scanner NSScanner * Scanner = [NSScanner scannerWithString: fileString]; // Ignore new lines [scanners set left for accounters: [NSCharacterSet CharacterSatWithersActors: @ "\ n"]]; NSString * sNumber = @ ""; NSString * sTitle = @ ""; // goes here and this expression is never correct ([Scanner Scan String: @ "String: And S Number] & Scanner Scan String: @", "In String: And Acetyl]" {NSLog (@ "SNumber:% @ sTitle:% @", sNumber, sTitle);}

I am using sample data;

  A15Q Test1, F74443AAZ, Test2  

When I locate the code, I go into the segment of time and it stops only on it.

Not sure that Why start SNumber and sTitle for @ ""; Zero is a better default (or you can not start them).

[Scanner Scan String: @ ", String: & Amp; Foo] is expected to view commas and returns in foo (it works for continuity and (I think) Unicode wotsits).

You might want to do something else:

  1. Read everything before a comma.
  2. Quit on a comma.
  3. No Read everything before the line.
  4. Leave on the new line.

(And I need to do some stick for formatting to work, for some reasons.)

  while ([scanner scanautopterstring string: @ "," In String: & amp; SNumber] & [Scanner Scan String: @ "," In String: NULL] And [Scanner ScanApotostration: @ "\ n" String: & amp; Stealth] & amp; [Scanner Scan String: @ "\ n" InString: NULL]) {NSLog (@ "sNumber:% @ sTitle:% @", sNumber, sTitle); }  

Let's say that you are trying to parse CSV, its problems are:

  • You probably \ r \ n got a new one Want to treat as a line.
  • It handles empty lines or lines without any commas.
  • It does not handle quoted strings (like "a, b", "c, d, e, f" ).

Comments