[Solved] Is there a way to combine Java8 Optional returning a value with printing a message on null?

Use orElseGet: Optional<String> startingOptional = getMyOptional(); String finishingValue = startingOptional.orElseGet(() -> { System.out.println(“value not found”); return “”; }); Using .map(value -> value) is useless: transforming a value into itself doesn’t change anything. solved Is there a way to combine Java8 Optional returning a value with printing a message on null?

[Solved] Xcode unexpectedly found nil while unwrapping an Optional, but no idea why

Solved it. As the comments pointed out, the problem was that I was not accessing ViewController correctly. In order to access my ViewController outside of the ViewController class, I was creating a new instance of it by ViewController(). I solved it by putting the function inside the class and changing ViewController() part to self.ViewController. This … Read more