【问题标题】:Can't read custom file from NSInputStream无法从 NSInputStream 读取自定义文件
【发布时间】:2014-03-23 12:42:52
【问题描述】:

我正在尝试通过 NSInputStream 从我的文档目录中读取自定义文件以将其上传到 FTP 服务器。我使用的代码与 Apple 提供的 SimpleFTPSample 中演示的代码完全相同。只要文件为空,它似乎就可以正常工作,但一旦它包含数据,它就会失败。

这是我的代码。这就是我创建输入流的方式,我尝试了两种方式:

//Approach one: Init with path
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

//Approach two: Init with data
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
self.fileStream = [NSInputStream inputStreamWithData:fileData];

如果我用数据初始化流,当我在流上调用read 时会得到EXC_BAD_ACCESS (code=1, address=0x0)(代码截断如下),如果我使用它直接跳转到File read error 的路径。

filePath@"/var/mobile/Applications/94292A2A-37FC-4D8E-BDA6-A26963932AE6/Documents/1395576645.cpn"NSData 正确返回,有 806 个字节。

这是stream:handleEvent: 委托方法的一部分:

if (self.bufferOffset == self.bufferLimit) {
         NSInteger   bytesRead;
         bytesRead = [self.fileStream read: self.buffer maxLength: kSendBufferSize];

         if (bytesRead == -1) {
            if (kPrintsToConsole) NSLog(@"File read error");
         } else if (bytesRead == 0) {
            [self stopSending];
         } else {
            self.bufferOffset = 0;
            self.bufferLimit  = bytesRead;
         }
}

我有点卡住了。希望你们能帮帮我。运行 iOS 7.1 和 Xcode 5.1

【问题讨论】:

    标签: ios objective-c ftp nsstream nsinputstream


    【解决方案1】:

    您需要在阅读前调用 [self.fileStream open]。适用于文件和数据方法。

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 1970-01-01
      • 2020-02-09
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-10
      • 2020-05-10
      相关资源
      最近更新 更多