10

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.

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.

comrade
  • 4,590
  • 5
  • 33
  • 48
cnoon
  • 16,575
  • 7
  • 58
  • 66
  • So are we thinking this is a bug? Given the fact that this has sat open for this many days I think it's about time to file a radar. – cnoon Feb 06 '15 at 07:10
  • 1
    I filed the following [radar](http://openradar.appspot.com/radar?id=4504359098384384). Please dupe if you have the same problem. If you don't and figured it out, please help! – cnoon Feb 12 '15 at 07:02
  • I encountered the same problem. Frankly, it also strikes me as a bug that I have to call one of their designated initializers (e.g. with `NSData` or `NSURL`) when the entire purpose of subclassing is that I probably don't want to use either of those. Frankly, subclassing `NSInputStream` has always been problematic (see http://blog.bjhomer.com/2011/04/subclassing-nsinputstream.html), but I was hoping they had looked at this over [the last 8 years](http://lists.apple.com/archives/macnetworkprog/2007/May/msg00056.html). – Rob May 21 '15 at 20:59
  • I'm running into this on an Objective-C project as well, so it's not Swift-specific. – Siobhán Aug 29 '15 at 02:07
  • FYI, I've tested it under Xcode 7 (beta 6). There's no issue. – BPCorp Aug 31 '15 at 14:24
  • That's awesome! Thanks for the heads up @BPCorp. – cnoon Sep 02 '15 at 15:15
  • I'm still getting the error with Xcode Version 7.1.1 (7B1005). – dontocsata Dec 03 '15 at 19:30

1 Answers1

0

I have found a solution using 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.

Xingxing
  • 580
  • 1
  • 6
  • 17