Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Created December 19, 2020 11:43
Show Gist options
  • Save ytyubox/539f4f0f0e11a8f873acc182596ab937 to your computer and use it in GitHub Desktop.
Save ytyubox/539f4f0f0e11a8f873acc182596ab937 to your computer and use it in GitHub Desktop.
@MustInjected replace var:!

init injection

class DIFormInit {
  let i: Int
  init(i: Int) {
    self.i = i
  }
}

must have by Optional

class DIFormProperty {
  var i: Int!
}

Must Injection

class DIFormMustHave {
  @MustInjected(file: #file, line: #line) 
  var i: Int
}
@propertyWrapper struct MustInjected<Value> {
private var _value: Value! = nil
#if DEBUG
let line: UInt
let file: StaticString
#endif
@available(*, unavailable)
init(wrappedValue: @autoclosure @escaping () -> Value) {
self._value = nil
line = #line
file = #file
}
var wrappedValue: Value {
mutating get {
#if DEBUG
precondition(_value != nil, "This property must be inject before access", file: file, line: line)
#endif
return _value
}
set {
#if DEBUG
precondition(_value == nil, "This property is actcully a Let property", file: file, line: line)
#endif
_value = newValue
}
}
}
extension MustInjected {
init(file: StaticString, line: UInt) {
self._value = nil
#if DEBUG
self.file = file
self.line = line
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment