VRAwesome
VRAwesome

Reputation: 4803

'self' used before super.init call in swift 3.0

I have sub class of OutputStream. In that I have two init() methods one have prefix word convenience.

Here is my code :

class FileOutputStream : OutputStream
{
      fileprivate let filepath:URL
      fileprivate let channel:DispatchIO!

      convenience init?(filename:String) {

           let pathURL = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in:.userDomainMask).first!.appendingPathComponent(filename)
           self.init(filepath:pathURL)
      }

      init?(filepath f:URL) {

           self.filepath = f

           //if let path = f.path,
           if let cpath = (f.path).cString(using: String.Encoding.utf8) {

                let outputflag:Int32 = O_CREAT | O_WRONLY               // create, write-only
                let mode:mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH // permissions: u+rw, g+r, o+r
                self.channel = DispatchIO(type: DispatchIO.StreamType.stream, path:cpath, oflag:outputflag,mode: mode, queue: DispatchQueue.global(qos: DispatchQoS.QoSClass.background)) { (errcode:Int32) -> Void in

                     if errcode != 0 {
                          print("FileOutputStream: error creating io channel")
                     }
                 }
            }
            else {
               self.channel = nil
               return nil
            }
    }

    func write(_ string: String) {

        if let dataString = string.data(using: String.Encoding.utf8) {

            dataString.withUnsafeBytes {(bytes: UnsafePointer<UInt8>) -> Void in

                var data = DispatchData.empty
                data.append(bytes, count: dataString.count)

                    self.channel.write(offset: 0, data: data, queue: DispatchQueue.global(qos:.background), ioHandler: { (complete: Bool, data: DispatchData?, errorCode: Int32) in

                        //handle progress reporting here
                         if errorCode != 0 {
                            print("FileOutputStream: error writing data to channel")
                        }
                    })
                }
            }
        }

    deinit {

        self.channel.close(flags: DispatchIO.CloseFlags.stop)
    }
}

I am getting error

'self' used before super.init call

I have tried by writing

super.init() 

But still getting error. If anyone know, please help me.

Upvotes: 0

Views: 2054

Answers (1)

LC 웃
LC 웃

Reputation: 18998

Use super.init(url: f, append: false/true) at the end of your init?(filepath f:URL) method .

Your intializer must call designated intializer of super class..Call any of these init methods that is available in OutputStream class...

public init(toMemory: ())
public init(toBuffer buffer: UnsafeMutablePointer<UInt8>, capacity: Int)
@available(iOS 4.0, *)
public init?(url: URL, append shouldAppend: Bool)

Upvotes: 2

Related Questions