Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用RubyMotion进行指针处理_Objective C_Pointers_Rubymotion - Fatal编程技术网

Objective c 使用RubyMotion进行指针处理

Objective c 使用RubyMotion进行指针处理,objective-c,pointers,rubymotion,Objective C,Pointers,Rubymotion,我正在尝试将以下方法移植到RubyMotion - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { NSInteger dataLength = [data length]; const uint8_t * dataBytes = [data bytes]; NSInteger bytesWritten; NSInteger

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

这就是我目前所拥有的,它在调用downloadStream时崩溃,带有
连接:didReceiveData:':无法将Fixnum转换为字符串(TypeError)


我意识到我的转换忽略了指针,这可能是幼稚和错误的。我已经查看了RubyMotion文档,但是它们有点稀疏,而且我对C的理解还不足以知道如何在这里应用它。一些建议将不胜感激。

NSData的
字节
方法
返回
指针
类型()

指针
类型中的
[]
方法允许您从指针的开始位置访问该位置的元素。在我看来,
字节
返回类型为“C”(无符号字符)的
指针
,因此当您尝试访问
指针
时,就像
数据字节[BytesWritesTensofar]
一样,您只需获取一个
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
我没有运行它,但我希望它能运行

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