自iOS 8.3更新以来,NSOutputStream停止工作

时间:2015-02-19 16:06:15

标签: ios objective-c swift

为什么以下代码class Test不接收流事件:

class Test
    : NSObject
    , NSStreamDelegate
{
    // NSStreamDelegate
    func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
        // this was never called
    }

    override init() {
        super.init()

        let filePath = /*[some valid file path]*/
        if let stream = NSOutputStream(toFileAtPath: filePath, append: false) {
            stream.delegate = self
            stream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode) // I've also tried replacing currentRunLoop with mainRunLoop just in case the result was the same
            stream.open()
            _stream = stream

            println("Stream opened \(_stream!.hasSpaceAvailable) \(_stream!.streamStatus.rawValue) \(_stream!.streamError)") // here hasSpaceAvailable is true, stream status is opened and error is nil
        }

    }

    deinit {
        _stream!.close()
    }

    func write() {
        if (_stream!.hasSpaceAvailable) {
            let data = NSMutableData(length: 10000)
            _stream!.write(UnsafePointer<UInt8>(data!.bytes), maxLength: data!.length)
        }
    }

    var _stream: NSOutputStream?
}

该类未被破坏(我将它作为ViewController成员)。难道我做错了什么?我无法在代码中发现问题,而且我无法在iOS 8.3,XCode 6.3发行说明中找到与我的问题相关的任何内容。我没有在Objective-C上尝试过,但很可能会出现同样的问题。

总是非常感谢帮助!

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,我把这个类设为私有,我想因为我设置为流的委托,调用功能无法看到它而只是没有调用它。