swift - All stored properties of a class instance must be initialized before returning nil from an initializer -


i'm trying use code in class though keep on getting above message.

    let filepath: nsstring!     let _filehandle: nsfilehandle!     let _totalfilelength: cunsignedlonglong!     init?(filepath: string) {       if let filehandle = nsfilehandle(forreadingatpath: filepath)     {          self.filepath = filepath         self._filehandle = nsfilehandle(forreadingatpath: filepath)         self._totalfilelength = self._filehandle.seektoendoffile()     }     else     {          return nil  //the error on line     } } 

how fix don't error:

all stored properties of class instance must initialized before returning nil initializer

you can make work variables , call super.init() (for creating self before accessing properties):

class test: nsobject {     var filepath: nsstring!     var _filehandle: nsfilehandle!     var _totalfilelength: cunsignedlonglong!      init?(filepath: string) {         super.init()         if let filehandle = nsfilehandle(forreadingatpath: filepath)         {             self.filepath = filepath             self._filehandle = nsfilehandle(forreadingatpath: filepath)             self._totalfilelength = self._filehandle.seektoendoffile()         }         else         {             return nil         }     } } 

but if plan stick version constants, it's out of comfort zone, , maybe this answer of help.


Popular posts from this blog