CHCSV错误:无法为长度分配内存。

3

我想解析一个 .csv 文件。为此,我使用 CHCSV 解析器。但是当我将其推入视图以开始解析时,应用程序崩溃。

由于未捕获的异常 'NSMallocException',应用程序终止,原因是: '* -[NSConcreteMutableData appendBytes:length:]: 无法为长度(4294967295)分配内存'

NSString *filePath = @"http://somewhere.com/test.csv";
NSString *fileContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:filePath] encoding:NSUTF8StringEncoding error:nil];
self.csvParser = [[CHCSVParser alloc] initWithContentsOfCSVFile:fileContent];


编辑:

我正在为iOS6+开发。感谢大家的评论和回答。我希望能得到正确的解决方案。

输入流
它不能工作。当我想使用输入流时,应用程序会因错误的编码而崩溃。

不兼容的整数转换为指针,将'int'发送到类型为'NSStringEncoding *'(也称为'unsigned int *')的参数

NSData *downloadData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://example.com/test.csv"]];
NSInputStream *stream = [NSInputStream inputStreamWithData:downloadData];
self.csvParser = [[CHCSVParser alloc] initWithInputStream:stream usedEncoding:NSUTF8StringEncoding delimiter:@";"];

self.csvParser.delegate = self;
[self.csvParser parse];



CSV字符串

NSString *filePath = @"http://example.com/test.csv";
NSString *fileContent = [NSString stringWithContentsOfURL:[NSURL URLWithString:filePath] encoding:NSUTF8StringEncoding error:nil];  
self.csvParser = [[CHCSVParser alloc] initWithCSVString:fileContent];

self.csvParser.delegate = self;
[self.csvParser parse];

此解析仅适用于(null)

我检查了大型和非常小的.csv文件,大小只有2KB。 - Florian
很奇怪...我刚试过那个示例项目,针对小文件和大文件都完美运行。 - HAS
在我的示例项目中,它也不起作用。 - Florian
是的。适用于iOS 6及以上版本。为什么? - Florian
我只是猜测了一下..现在我能够重现你的问题..我会寻找解决方案。 - HAS
显示剩余2条评论
1个回答

2
最终编辑: CHCSVParser的作者Dave在github上更新了他的代码,因此当您使用最新版本时,这个问题应该得到解决。立即获取!

好的,我们开始吧:

首先,在CHCSVParser.m中添加以下代码:

在方法- (void)_sniffEncoding的开头,您有:

uint8_t bytes[CHUNK_SIZE];
NSUInteger readLength = [_stream read:bytes maxLength:CHUNK_SIZE];
[_stringBuffer appendBytes:bytes length:readLength];
[self setTotalBytesRead:[self totalBytesRead] + readLength];

将其更改为:

uint8_t bytes[CHUNK_SIZE];
NSUInteger readLength = [_stream read:bytes maxLength:CHUNK_SIZE];
if (readLength > CHUNK_SIZE) {
    readLength = CHUNK_SIZE;
}
[_stringBuffer appendBytes:bytes length:readLength];
[self setTotalBytesRead:[self totalBytesRead] + readLength];

之后我只得到了null值,所以我改变了file路径(在示例项目中,它位于main()中,但我在viewDidLoad中进行了解析。

确保您将文件复制到捆绑目录中才能正常工作!

file = [NSBundle pathForResource:@"Test" ofType:@"scsv" inDirectory:[[NSBundle mainBundle] bundlePath]];

编辑:

当你说你需要下载文件时,可以执行以下操作(但请注意,这是一种快速而不太完美的解决方案,特别是在移动设备上)。

NSData *downloadData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.yourdomain.tld/Test.scsv"]];
NSInputStream *stream = [NSInputStream inputStreamWithData:downloadData];

这里最重要的是你需要更改最后一行。

希望这解决了你的问题。

编辑2:

我刚刚为你创建了一个演示项目的存储库,其中代码实际上是有效的。也许你可以找出你做错(或至少不同)的地方。这是链接。

编辑3:

更改

self.csvParser = [[CHCSVParser alloc] initWithInputStream:stream usedEncoding:NSUTF8StringEncoding delimiter:@";"];

为了

self.csvParser = [[CHCSVParser alloc] initWithInputStream:stream usedEncoding:&encoding delimiter:';'];

感谢您的出色回答。文件必须在服务器上。因此,使用bundle的解决方案并不适合。为什么只有我遇到这个问题? - Florian
但那不应该是问题 - 重要的部分是您插入 if 条件的部分。 - HAS
3
(CHCSVParser的作者在这里)有趣,我想我应该将“readLength”更改为有符号数,并检查确保它不是负数。谢谢! - Dave DeLong
1
感谢您分享您的示例项目!这解决了编码问题! - Florian
2
我已经更新了CHCSVParser代码来解决这个问题:https://github.com/davedelong/CHCSVParser 谢谢你发现它! - Dave DeLong
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接