iphone - CHCSV 错误 : unable to allocate memory for length

标签 iphone ios objective-c csv

我想解析一个 .csv 文件。为此,我使用 CHCSV 解析器。但是当我进入解析器应该开始解析的 View 时,应用程序崩溃了。

Terminating app due to uncaught exception 'NSMallocException', reason: '* -[NSConcreteMutableData appendBytes:length:]: unable to allocate memory for 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];


编辑:

我正在为 iOS 6+ 开发。感谢您的精彩评论和回答。我希望得到正确的解决方案。

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

Incompatible integer to pointer conversion sending 'int' to parameter of type 'NSStringEncoding *' (aka '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)

最佳答案

最终编辑: CHCSVParser 的作者 Dave 在 github 上更新了他的代码,因此当您使用最新版本时应该可以解决此问题。 Get it now!


好的,我们开始:

首先在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:

我刚刚为您创建了一个包含演示项目的存储库,代码可以在其中实际运行。也许你可以找出你做错了什么(或至少不同)。 Here is the link.

编辑 3:

改变

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

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

关于iphone - CHCSV 错误 : unable to allocate memory for length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16132656/

相关文章:

iphone - NSTimers 和 NSRunLoops 的不稳定行为

ios - 在离线 Google Map iOS 上集成 KML

ios - IOS 外部 GPS 数据的 WGS84 Geoid Height Altitude Offset

iphone - iphone 和 ipad 之间 modalviewcontroller 的不同方向行为

ios - 什么 View 对象是 Apple 日历应用程序 (iOS/OS X) 的基础

iphone/ios adHoc 分发崩溃(仅在 adHoc 分发时关闭)

ios - 在展开 Optional 值时意外发现 nil? swift 4

iphone - UITableView 单元格中的 UILabel 在 iOS7 中相互重叠

ios - XCode - block 完成时执行代码

objective-c - 带分页和动量的 UITableView