Giter VIP home page Giter VIP logo

Comments (3)

semireg avatar semireg commented on August 16, 2024

Here's the code I'm using in my delegate.

- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex
{
    // SKIP: Undefined keyCol and valueCol (both are likely 0)
    if(self.keyCol == self.valueCol)
        return;

    // Set the key in temporary keyObject
    if(fieldIndex == self.keyCol)
    {
        self.keyObject = field;
    }

    // Set the key --> value pair
    if(fieldIndex == self.valueCol && self.keyObject && field)
    {
        [self.mapDict setObject:field forKey:self.keyObject];

        // Reset key (value is temporary)
        self.keyObject = nil;
    }
}

from chcsvparser.

semireg avatar semireg commented on August 16, 2024

And...

-(NSDictionary*)dictionaryWithKeysColumn:(NSInteger)keyColumn valueColumn:(NSInteger)valueColumn
{
    self.mapDict = [[NSMutableDictionary alloc] init];
    self.keyCol = keyColumn;
    self.valueCol = valueColumn;
    [self parse];

    self.keyCol = 0;
    self.valueCol = 0;

    return self.mapDict;
}

-(void)parse
{
    self.csvParser = nil;
    [self.csvParser parse];
}

-(CHCSVParser*)csvParser
{
    if(!_csvPathForResource)
    {
        NSLog(@"%s - error, missing csvPathForResource", __PRETTY_FUNCTION__);
        return nil;
    }

    if(!_csvParser)
    {
        NSString *csvFilePath = [[NSBundle mainBundle] pathForResource:self.csvPathForResource
                                                                ofType:@"csv"
                                                           inDirectory:self.csvDirForResource];

        if(csvFilePath)
            _csvParser = [[CHCSVParser alloc] initWithContentsOfCSVFile:csvFilePath];

        NSLog(@"%s - created _csvParser:%@", __PRETTY_FUNCTION__, _csvParser);
    }

    return _csvParser;
}

from chcsvparser.

semireg avatar semireg commented on August 16, 2024

DOH. I was on the right track... Deallocating works. However, my code above didn't set the delegate back to self. I added one more line to the lazy loading -csvParser method.

-(CHCSVParser*)csvParser
{
    if(!_csvPathForResource)
    {
        NSLog(@"%s - error, missing csvPathForResource", __PRETTY_FUNCTION__);
        return nil;
    }

    if(!_csvParser)
    {
        NSString *csvFilePath = [[NSBundle mainBundle] pathForResource:self.csvPathForResource
                                                                ofType:@"csv"
                                                           inDirectory:self.csvDirForResource];

        if(csvFilePath)
        {
            _csvParser = [[CHCSVParser alloc] initWithContentsOfCSVFile:csvFilePath];
            _csvParser.delegate = self;
        }           

        NSLog(@"%s - created _csvParser:%@", __PRETTY_FUNCTION__, _csvParser);
    }

    return _csvParser;
}

from chcsvparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.