ios-NSOutputStream不';不写数据
我正在下载文件并将其字节写入ios-NSOutputStream不';不写数据,ios,objective-c,nsoutputstream,Ios,Objective C,Nsoutputstream,我正在下载文件并将其字节写入NSOutputStream: self.outputFileStream = [NSOutputStream outputStreamToFileAtPath:downloadFilePath append:NO]; [self.outputFileStream setDelegate:self]; 然后,在nsursession的didReceiveResponse回调中: [self.outputFileStream open]; 并且,一旦
NSOutputStream
:
self.outputFileStream = [NSOutputStream outputStreamToFileAtPath:downloadFilePath
append:NO];
[self.outputFileStream setDelegate:self];
然后,在nsursession
的didReceiveResponse
回调中:
[self.outputFileStream open];
并且,一旦接收到第一个字节:
const uint8_t *bytes = (const uint8_t*)[data bytes];
NSInteger bytesLeft = [data length];
NSInteger bytesWritten = -1;
do {
bytesWritten = [self.outputFileStream write:bytes maxLength:bytesLeft];
if (bytesWritten == -1) {
break;
}
bytesLeft -= bytesWritten;
} while (bytesLeft > 0);
当我第一次尝试将数据写入流时,执行停止:write:maxLength:
返回-1
为了找出发生这种情况的原因,我实现了一个委托方法handleEvent:eventCode:
出于某种原因,只有在初始化流时放置以下代码行时,才会调用它:
[self.outputFileStream scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
并返回nsstreamventerrocurred
,其中没有此类文件或目录
说明,尽管该目录已被双重检查为存在