我正在尝试将以下方法移植到 RubyMotion
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSInteger dataLength = [data length];
const uint8_t * dataBytes = [data bytes];
NSInteger bytesWritten;
NSInteger bytesWrittenSoFar;
bytesWrittenSoFar = 0;
do {
bytesWritten = [self.downloadStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
assert(bytesWritten != 0);
if (bytesWritten == -1) {
[self cleanupConnectionSuccessful:NO];
break;
} else {
bytesWrittenSoFar += bytesWritten;
}
} while (bytesWrittenSoFar != dataLength);
self.progressContentLength += dataLength;
}
(来自robertmryan/download-manager)
这就是我目前所拥有的,它在调用 downloadStream 时崩溃connection:didReceiveData:': can't convert Fixnum into String (TypeError)
def connection(connection, didReceiveData:data)
dataLength = data.length
dataBytes = data.bytes
bytesWritten = 0
bytesWrittenSoFar = 0
begin
maxLength = dataLength - bytesWrittenSoFar
buffer = dataBytes[bytesWrittenSoFar]
bytesWritten = self.downloadStream.write buffer, maxLength: maxLength # CRASH
if bytesWritten == -1
self.cleanupConnectionSuccessful false
break
else
bytesWrittenSoFar += bytesWritten
end
end while bytesWrittenSoFar != dataLength
self.progressContentLength += dataLength
if self.delegate.respondsToSelector('downloadDidReceiveData:')
self.delegate.downloadDidReceiveData(self)
end
end
我意识到我的转换忽略了指针可能是幼稚和错误的。我检查了 RubyMotion 文档,但它们有点稀疏,而且我对 C 的理解还不够强,无法知道如何在此处应用它。一些建议将不胜感激。