在NSMutableData中附加从Web下载的数据

时间:2013-02-02 10:28:44

标签: iphone ios objective-c csv nsmutabledata

我正在尝试从我正在从互联网上获取的CSV文件中附加数据。出了点问题,我无法弄清楚是什么。我的日志没有为“Field”或“Value”返回任何内容。我将数据保存为NSMutableData并尝试追加它。我以任何方式覆盖它吗?数据存储/检索过程中的某些内容必定是错误的......你能不能看看这段代码并告诉我,我做错了什么:

#import "ViewController.h"
#import "CHCSVParser.h"

@interface ViewController ()
@end
@implementation ViewController
@synthesize parser = _parser;
bool isStatus;
@synthesize apendData;

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
  NSLog(@"connectionDidFinishLoading");
    NSString *myString;
    myString = [[NSString alloc] initWithData: apendData encoding:NSUTF8StringEncoding];
    _parser = [[CHCSVParser alloc] initWithContentsOfCSVFile:[NSHomeDirectory() stringByAppendingPathComponent:myString]];
    _parser.delegate = self;
     [_parser parse];
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"data: %@", data);
   if (apendData)
        [apendData appendData:data];
    else
        apendData = [[NSMutableData alloc] initWithData:data];
}
- (void)loadDatafromURL
{
  NSURL *url = [NSURL URLWithString:@"http://www.google.com/trends/trendsReport?hl=en-US&cat=0-13&date=today%207-d&cmpt=q&content=1&export=1"];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
   [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"%@", [error description]);
}
- (void)parser:(CHCSVParser *)parser didBeginLine:(NSUInteger)recordNumber
{
    if (recordNumber == 41) {
        isStatus = YES;
        NSLog(@"ParsingStart..."); // Not getting this
    }
}
- (void)parser:(CHCSVParser *)parser didEndLine:(NSUInteger)recordNumber
{
    if (recordNumber == 50) {
        NSLog(@"ParsingEnd..."); // Not getting this
        [parser cancelParsing];
    }
}
- (void)parser:(CHCSVParser *)parser didReadField:(NSString *)field atIndex:(NSInteger)fieldIndex
{
    if ((isStatus = YES)) {
        NSLog(@"YES is working");
        if (fieldIndex == 0) {
            NSLog(@"Field = %@", field); // Just getting "Field = "
        }
        if (fieldIndex == 1){
            NSLog(@"Value = %@", field); // Not getting this
        }
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadDatafromURL];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

0 个答案:

没有答案
相关问题