我正在尝试在类中使用此代码,但我继续获取上述消息.
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 is on this line } }
All stored properties of a class instance must be initialized before
returning nil from an initializer
解决方法
您可以使用变量和调用super.init()(在访问其属性之前创建self):
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 } } }
但是,如果你打算坚持你的版本与常量,那么它是我的舒适区,也许this answer可能会有所帮助.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。