Does it make sense to have a non-optional weak reference?

Viewed 750

Consider this code:

internal weak var someObj: SomeClass? = TheObject.Exists().Somewhere.obj

This seems perfectly reasonable. The reference is weak - that is we have no control over someObj's lifetime - and so at some point the reference may become nil. Even SomeClass! could me regarded as acceptable, as long as you are aware of the possible risk. Now how about this:

internal weak var anotherObj: AnotherClass = aDifferent.Source().ofObjects.obj

The compiler does not even issue a warning. Weak suggests the reference may point at an object which had been deallocated. On the other hand the non-optional AnotherClass type doesn't really allow that. Can such code be justified?

1 Answers
Related