gpt4 book ai didi

iphone - 尝试将 NSData 从 NSURLConnection 写入文件后的 SIGABRT(无法识别的选择器)

转载 作者:可可西里 更新时间:2023-11-01 05:47:55 25 4
gpt4 key购买 nike


已解决:文件名是一个自动释放的字符串,在调用 createFileAtPath 时不再可用:


我正在尝试跟踪文件的下载进度,我正在尝试实现的代码是这样的(已编辑):

 connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

if (connection)
receivedData = [[NSMutableData data] retain];

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];

totalBytes = [[NSNumber numberWithLongLong:[response expectedContentLength]] intValue];
NSLog(@"content-length: %i bytes", totalBytes);
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];

int resourceLength = [[NSNumber numberWithUnsignedInteger:[receivedData length]] intValue];
NSLog(@"receivedData length: %i", resourceLength);
}

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[fileMgr createFileAtPath:filename contents:receivedData attributes:nil];

//if instead i write only the Apple example:
//NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
//there's no SIGABRT
[receivedData release];
[connection release];
}

但是 createFileAtPath:contents:attributes: 只给出 SIGABRT:

-[__NSCFData getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x2ba510

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0x2ba510'

我做错了什么?这不是异步下载文件的方式吗?

还有 2 件事:a) 内容长度 NSLog 是正确的。 b) 如果我不在 didReceiveResponse 中使用 initWithCapacity:content-lenght 而只是初始化,则 receivedData 长度只会增长大约内容长度的两倍...

最佳答案

已解决:文件名是一个自动释放的字符串,在调用 createFileAtPath 时不再可用:

关于iphone - 尝试将 NSData 从 NSURLConnection 写入文件后的 SIGABRT(无法识别的选择器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com