gpt4 book ai didi

objective-c - 使用 RubyMotion 处理指针

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:36 26 4
gpt4 key购买 nike

我正在尝试将以下方法移植到 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)

这就是我目前拥有的,它在使用 connection:didReceiveData:': can't convert Fixnum into String (TypeError)

调用 downloadStream 时崩溃
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 的理解还不够强,不知道如何在此处应用它。一些建议将不胜感激。

最佳答案

NSDatabytes 方法返回一个Pointer 类型(http://www.rubymotion.com/developer-center/api/Pointer.html)。

Pointer 类型中的 [] 方法使您可以从指针开始访问该位置的元素。在我看来,bytes 返回类型为“C”(unsigned char)的Pointer,所以当您尝试访问 Pointer 时,dataBytes[bytesWrittenSoFar] 你只获得了一个 Fixnum,它是从指针开始的那个字节的值。

要完成您想做的事,您需要以下内容:

bytesWrittenSoFar = 0

begin
maxLength = dataLength - bytesWrittenSoFar
buffer = dataBytes + bytesWrittenSoFar
bytesWritten = self.downloadStream.write buffer, maxLength: maxLength
if bytesWritten == -1
self.cleanupConnectionSuccessful false
break
else
bytesWrittenSoFar += bytesWritten
end
end while bytesWrittenSoFar != dataLength

我还没有运行它,但我希望它有效。

关于objective-c - 使用 RubyMotion 处理指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17001406/

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