简体   繁体   English

无法从Swift继承NSInputStream(initWithData:unrecognizer selector)

[英]Failing to subclass NSInputStream from Swift (initWithData: unrecognizer selector)

I can't seem to figure out what I am doing wrong to produce the following error: 我似乎无法弄清楚我做错了产生以下错误:

2015-02-02 12:48:17.029 InputStreams[14816:221224] -[InputStreams.CustomStream initWithData:]: unrecognized selector sent to instance 0x7fda2e1aac30

Here is my CustomStream subclass. 这是我的CustomStream子类。

import Foundation

class CustomStream : NSInputStream {
    let streamName = "My Custom Stream"

    override init(data: NSData) {
        super.init(data: data)
    }
}

And here's a quick example of how I'm trying to instantiate it: 以下是我试图实例化它的一个简单示例:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let stream = CustomStream(data: NSData())
        println("Stream Name: \(stream.streamName)")
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。

I have found a solution using convinence init. 我找到了一个使用convinence init的解决方案。

class CustomStream : NSInputStream {
    let streamName = "My Custom Stream"
    // var data:NSData!        

    convenience override init(data: NSData) {
        self.init()
        // do inialization.
        // self.data = data 
    }

    convenience init() {
        self.init()
    }
}

To be honest I have not figured out why it works.I am referring to Class Inheritance and Initialization for more information.Please leave a comment if you have any idea about it. 说实话,我还没弄清楚它为什么会起作用。我指的是类继承和初始化以获取更多信息。如果你对它有任何想法,请发表评论。

One more thing,it is considered safer to use CFCreateBounderPair rather than to subclass NSInputStream .I have tried to convert ALAsset to NSInputStream successfully in both ways successfully.The code is available here ALAssetToNSInputStream . 还有一件事,使用CFCreateBounderPair而不是子类NSInputStream被认为更安全。我试图在两种方式下成功地将ALAsset转换为NSInputStream 。此处的代码可用于ALAssetToNSInputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM