The nil coalescing operator


(Valentin Radu) #1

Hello all,

I have a question re the nil coalescing operator. Shouldn’t

print ("This machine's ip is \(result["ip"] ?? “unknown")”)

return This machine's ip is 78.97.202.31 ? Since the value is always String, not String?. However, it still surrounds the ip with Optional().

So I’m using, (result.0["ip"] ?? "nil").description but is long and dirty. Any thoughts?

Cheers!
– V


(Jordan Rose) #2

That does seem like a bug, but one possibility is that 'result' has type 'AnyObject', and so you're getting an extra level of optional due to the subscript operator possibly not being there. Do you have a full, self-contained test case?

Jordan

···

On Feb 22, 2016, at 15:17 , Valentin Radu via swift-users <swift-users@swift.org> wrote:

Hello all,

I have a question re the nil coalescing operator. Shouldn’t

print ("This machine's ip is \(result["ip"] ?? “unknown")”)

return This machine's ip is 78.97.202.31 ? Since the value is always String, not String?. However, it still surrounds the ip with Optional().

So I’m using, (result.0["ip"] ?? "nil").description but is long and dirty. Any thoughts?

Cheers!
– V
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users