createUDPSession from:fromEndpoint

func createUDPSession(to remoteEndpoint: NWEndpoint, from localEndpoint: NWHostEndpoint?) -> NWUDPSession

The docs say, "fromEndpoint, The local endpoint to bind the UDP session to. If nil, the UDP session will be bound to an ephemeral port on the primary physical interface."

I can't seem to figure out how to pass a nil without getting an "Fatal error: Unexpectedly found nil while unwrapping an Optional value".  How do I pass a nil without failing and getting this error?

The error you’re getting is a Swift runtime error, which is weird because the classes involved (NEProvider and its various subclasses, NWEndpoint and its various subclasses, NWUDPSession) are all Objective-C. Thus, the error isn’t coming from the framework but from your call, and it’s hard to see how that might be happening.

It’s possible that there’s an nullability annotation problem here, that is, createUDPSession(…) is returning nil even though it’s annotated not to. Try doing this:

guard let session = provider.createUDPSession(to: endpoint, from: nil) as NWUDPSession? else {
    // line A
}
// line B

and see whether you end up at line A or B.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Terms of Service

Privacy Policy

Cookie Policy