Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing CSV: how can NSScanner recognize empty field (i.e. ,,)?
    primarykey
    data
    text
    <p>I am very new to Xcode and trying - as millions - to parse a CSV file. I have read many contributions and I am managing it but I have a problem when my NSScanner intercepts an empty field: "Field_A, Field_B,, Field_D". I guess it is because it ignores empty space by default, or in this case no space at all.</p> <p>String is:</p> <p>"Personal","2011-01-01","Personal","Cigarettes",,4.60,"Cash","",</p> <p>I tried to debug it using scanLocation:</p> <pre><code>2011-04-22 15:57:32.414 Spending[42015:a0f] Before while...scan location is:0 2011-04-22 15:57:32.414 Spending[42015:a0f] Account: "Personal" - scan location is:10 2011-04-22 15:57:32.415 Spending[42015:a0f] Date: "2011-01-01" - scan location is:23 2011-04-22 15:57:32.415 Spending[42015:a0f] Category: "Personal" - scan location is:34 2011-04-22 15:57:32.416 Spending[42015:a0f] Subcategory: "Cigarettes" - scan location is:47 2011-04-22 15:57:32.416 Spending[42015:a0f] Income: 4.600000 - scan location is:53 2011-04-22 15:57:32.416 Spending[42015:a0f] Expense: 0.000000 - scan location is:53 2011-04-22 15:57:32.417 Spending[42015:a0f] Payment: "Cash" - scan location is:60 2011-04-22 15:57:32.417 Spending[42015:a0f] Note: "" - scan location is:63 </code></pre> <p>And as you can see after that even expense field gets no value (should be 4.60).</p> <p>Here is the relevant piece of code:</p> <pre><code>NSScanner *scanner = [NSScanner scannerWithString:fileString]; [scanner setCharactersToBeSkipped: [NSCharacterSet characterSetWithCharactersInString:@"\n, "]]; NSString *account, *date, *category, *subcategory, *payment, *note; float income, expense; // Set up data delimiter using comma NSCharacterSet *commaSet; commaSet = [NSCharacterSet characterSetWithCharactersInString:@","]; NSLog (@"Before while...scan location is:%d\n", scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;account]; NSLog(@"Account: %@ - scan location is:%d\n",account, scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;date]; NSLog(@"Date: %@ - scan location is:%d\n",date, scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;category]; NSLog(@"Category: %@ - scan location is:%d\n",category, scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;subcategory]; NSLog(@"Subcategory: %@ - scan location is:%d\n",subcategory, scanner.scanLocation); [scanner scanFloat:&amp;income]; NSLog(@"Income: %f - scan location is:%d\n",income, scanner.scanLocation); [scanner scanFloat:&amp;expense]; NSLog(@"Expense: %f - scan location is:%d\n",expense, scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;payment]; NSLog(@"Payment: %@ - scan location is:%d\n",payment, scanner.scanLocation); [scanner scanUpToCharactersFromSet:commaSet intoString:&amp;note]; NSLog(@"Note: %@\n - scan location is:%d",note, scanner.scanLocation); </code></pre> <p>I tried looking carefully through NSScanner Class Reference, but could not get an idea? Do you have any? </p> <p>Thanks, Fabrizio.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload